Javascript jQuery-Change href attr

Javascript jQuery-Change href attr,javascript,jquery,html,href,attr,Javascript,Jquery,Html,Href,Attr,我试图通过jQuery更改href,但我不知道为什么它不起作用 $(document).ready(function () { var oldurl = $('`http://google.com`'); var newurl = $('`http://yahoo.com`'); $('a[href="' + oldurl + '"]').attr('href', newurl); // it's not working too... //$('a[href="http:

我试图通过jQuery更改
href
,但我不知道为什么它不起作用

$(document).ready(function () {
  var oldurl = $('`http://google.com`');
  var newurl = $('`http://yahoo.com`');

  $('a[href="' + oldurl + '"]').attr('href', newurl);

  // it's not working too...
  //$('a[href="http://google.com"]').attr('href', 'http://yahoo.com');

  // it's not working too...
  //$('.mylink').attr('href', newurl);
});
您只需要:

var oldurl = 'http://google.com';
var newurl = 'http://yahoo.com';
$('a[href="' + oldurl + '"]').attr('href', newurl);


URL必须是字符串,而不是
jQuery
对象,并且您忘记关闭方括号。

您没有关闭此行的方括号:
$('a[href=“”+oldurl+”)
对,我错过了,现在添加了,但仍然不起作用:您希望
$('`http://google.com`“)
要做什么?
$()
应该是DOM选择器(用于查找匹配元素)或HTML字符串(用于创建新元素)。您试图通过将URL放在那里来做什么,为什么要在其周围打勾?没什么,这只是我的错误。总之,感谢通过下面的答案解决了问题。谢谢。我没有DV,请参阅@rajarabhuarvindasamy,是的。完成了