gpt4 book ai didi

jquery - 对于 href 等于 myValue 的每个链接

转载 作者:太空狗 更新时间:2023-10-29 13:18:15 26 4
gpt4 key购买 nike

为什么以下内容没有将文本更改为有效?

//JAVASCRIPT/JQUERY:

$('a').each(function(i) {
if($(i).attr("href") == "mywebsite.co.uk")
{
$(i).innerHTML = "It Worked!";
}
});


//HTML:

<a href="mywebsite.co.uk"></a>

调试它似乎没有获取 href value.attr 但我可能弄错了有人可以确保我正确完成了上述操作吗?

最佳答案

i是元素的索引,你想要元素并且应该使用应该使用类似的东西:

// The first argument is the index, the second is the element
$('a').each(function(index, element) {
if($(element).attr("href") == "mywebsite.co.uk")
{
$(element).html("It Worked!"); // Change this to .html()
}
console.log('Index is:'+index+', Element is'+element);
});​

<a href="mywebsite.co.uk"></a>

此外,我更改了 .innerHtml().html("content in here") .更新返回的 <a></a> 中的 HTML标签 (element) .

检查这个JSFiddle . http://jsfiddle.net/2scug/1/

关于jquery - 对于 href 等于 myValue 的每个链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12456890/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com