Javascript 如何获取此.id的.index()
如果我想使用Javascript 如何获取此.id的.index(),javascript,jquery,Javascript,Jquery,如果我想使用each()遍历类。如何获取附加到该类的id,并获取其索引 我尝试这样做,但this.id只返回该id名称,而不是实际id: $('.nav click')。每个(函数(){ log($(this.id).index('.nav click'); }); 您可以使用此引用元素: $('.nav-click').each(function() { console.log(this.id); // the id attribute console.log($(this).inde
each()
遍历类。如何获取附加到该类的id
,并获取其索引
我尝试这样做,但this.id
只返回该id名称,而不是实际id:
$('.nav click')。每个(函数(){
log($(this.id).index('.nav click');
});
您可以使用此
引用元素:
$('.nav-click').each(function() {
console.log(this.id); // the id attribute
console.log($(this).index('.nav-click')); // the index of this element within the .nav-click set
});
或者,您也可以只使用传递给each()
处理程序函数的index
参数:
$('.nav-click').each(function(i) {
console.log(i);
});
您可以使用
index
和attr
函数
$('.nav click')。每个(函数(){
log($(this.index(),$(this.attr('id'));
});代码>
不是$(this.id)
,而是$(this)
。哇,肯定是想太多了。谢谢