为什么以下内容没有将文本更改为有效?
//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)
.
关于jquery - 对于 href 等于 myValue 的每个链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12456890/