如果我想使用迭代遍历一个类each().如何获得id附加到该类并获得它的索引?
我尝试这样做但this.id只返回id名称而不是实际id:
$('.nav-click').each(function() {
console.log($(this.id).index('.nav-click'));
});
Run Code Online (Sandbox Code Playgroud)
您可以使用它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
});
Run Code Online (Sandbox Code Playgroud)
或者,您可以使用index传递给each()处理函数的参数:
$('.nav-click').each(function(i) {
console.log(i);
});
Run Code Online (Sandbox Code Playgroud)