簡體   English   中英

如何獲取由 querySelectorAll() 方法(javascript)制作的 DOM 數組的每個元素的索引?

[英]How To get the Index of each element of the DOM array made by the querySelectorAll() method (javascript)?

let var = document.querySelectoAll('.class')

console.log(var) = Array[15] 

(數組由 15 個元素組成)

您可以使用NodeList.entries()

const nodes = document.querySelectorAll('.class');
for (const [index, node] of nodes.entries()) {
    console.log(index);
    console.log(node);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM