如果我想使用 each()
遍历一个类。我如何获取附加到该类的 id
并获取它的索引?
我试着这样做,但 this.id
只返回那个 id 名称而不是实际的 id:
$('.nav-click').each(function() {
console.log($(this.id).index('.nav-click'));
});
最佳答案
您可以只使用this
来引用该元素:
$('.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);
});
关于javascript - 如何获取 this.id 的 .index(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45152008/