Javascript 如何使用html表在每个函数中获取索引

Javascript 如何使用html表在每个函数中获取索引,javascript,jquery,Javascript,Jquery,我需要在每个具有class=索引的函数中使用索引 我想要的结果是2和4和5,它来自td元素基索引 如何获取td基本索引 如果你有意见,请告诉我 谢谢 $(“.index”)。每个(函数(){ 让index=$(this.index()+1 控制台日志(索引); }); 1. 2. 3. 4. 5. 6. 您可以在香草JavaScript中这样做: let index=document.querySelectorAll(“.index”); log(Array.from(index).map

我需要在每个具有class=
索引的函数中使用索引

我想要的结果是
2
4
5
,它来自
td
元素基索引

如何获取
td
基本索引

如果你有意见,请告诉我

谢谢

$(“.index”)。每个(函数(){
让index=$(this.index()+1
控制台日志(索引);
});

1.
2.
3.
4.
5.
6.

您可以在香草JavaScript中这样做:

let index=document.querySelectorAll(“.index”);
log(Array.from(index).map((i)=>i.textContent))

1.
2.
3.
4.
5.
6.

您可以在香草JavaScript中这样做:

let index=document.querySelectorAll(“.index”);
log(Array.from(index).map((i)=>i.textContent))

1.
2.
3.
4.
5.
6.
试试:
$(“.index”)。每个(函数(){
log($(this)[0].innerHTML);
});

它将使索引与td的值相同。

尝试:
$(“.index”)。每个(函数(){
log($(this)[0].innerHTML);
});

它将使索引与td的值相同。

$。each()
函数可以传递索引和当前html元素
$(“.index”)。each(函数(i,项)

您可以通过
text()
方法,
$(item)获取td内容。text()
将获取当前td文本

$(".index").each(function(i, item){
let index=$(item).text();
console.log(index);
});
$(“.index”)。每个(函数(i,项){
让索引=$(项).text();
控制台日志(索引);
});

1.
2.
3.
4.
5.
6.
$。each()
函数您可以传递索引和当前html元素
$(“.index”)。each(函数(i,项)

您可以通过
text()
方法,
$(item)获取td内容。text()
将获取当前td文本

$(".index").each(function(i, item){
let index=$(item).text();
console.log(index);
});
$(“.index”)。每个(函数(i,项){
让索引=$(项).text();
控制台日志(索引);
});

1.
2.
3.
4.
5.
6.
返回元素相对于其父元素的索引

的回调函数已接收匹配集合中元素的索引,因此,如果希望索引与表相关,则必须将选择器更改为更通用的选择器,并检查元素是否与您的条件匹配

$(“td”)。每个函数(idx){
如果($(this).is('.index')){
console.log(idx+1)
}
});

1.
2.
3.
4.
5.
6.
返回元素相对于其父元素的索引

的回调函数已接收匹配集合中元素的索引,因此,如果希望索引与表相关,则必须将选择器更改为更通用的选择器,并检查元素是否与您的条件匹配

$(“td”)。每个函数(idx){
如果($(this).is('.index')){
console.log(idx+1)
}
});

1.
2.
3.
4.
5.
6.

如果您想使用javascript并扩展操作符,请将其缩短

[...document.querySelectorAll(".index")].map((item, index)=>{
  console.log(item.textContent);
});
[…document.queryselectoral(.index”)].map((项,索引)=>{
console.log(item.textContent);
});

1.
2.
3.
4.
5.
6.

如果您想使用javascript并扩展操作符,请将其缩短

[...document.querySelectorAll(".index")].map((item, index)=>{
  console.log(item.textContent);
});
[…document.queryselectoral(.index”)].map((项,索引)=>{
console.log(item.textContent);
});

1.
2.
3.
4.
5.
6.

您得到了什么结果?您是否阅读了
.index()
方法的文档以了解它的功能?您得到了什么结果?您是否阅读了
.index()
方法的文档以了解它的功能?