我尝试用jquery更改3个a标签的href。 目前,我用普通的 javascript 来做到这一点。
var catNav = $('ul.nav'),
newLink = ['new1/','new2','nwe3/'];
catNav.attr('id','node');
var node =document.getElementById('node');
for(var i=0; i<3; i++){
node.childNodes[i].firstChild.href=newLink[i];
}
我想从你这里得到什么。只是教我如何做到这一点
for(var i=0; i<3; i++){
node.childNodes[i].firstChild.href=newLink[i];
}
使用jquery。
这是一个例子 http://jsfiddle.net/HFj2D/1/
最佳答案
这应该完成整个事情:
var newLink = ['new1/','new2','nwe3/'];
$("ul.nav").attr('id', 'node').find('a').each(function(index, el) {
this.href = newLink[index];
});
理想情况下,您应该已经在<ul>
上有一个ID不过无论如何都要标记。
关于javascript - 用 jquery 替换一堆 href。只需要改进代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8678316/