jQuery - 更改href attr

ITS*_*ion 2 html javascript jquery href attr

我试图href通过jQuery 改变,但我不知道为什么它不工作...

$(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);
});
Run Code Online (Sandbox Code Playgroud)

的jsfiddle

Ami*_*oki 6

您只需要:

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

演示

网址必须是字符串而不是jQuery对象,而您忘记关闭方括号.