簡體   English   中英

jQuery-更改href屬性

[英]jQuery - Change 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://google.com"]').attr('href', 'http://yahoo.com');

  // it's not working too...
  //$('.mylink').attr('href', newurl);
});

的jsfiddle

您只需要:

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

演示

Urls必須是字符串,而不是jQuery對象,並且您忘記了關閉方括號。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM