[英]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);
});
您只需要:
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.