Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/379.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何获取此.id的.index()_Javascript_Jquery - Fatal编程技术网

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)
。哇,肯定是想太多了。谢谢