简体   繁体   English

如何在 JS 中循环遍历 DOM 中的所有 nodeValues

[英]How to Loop through all nodeValues in DOM in JS

I am trying to loop through and increment the following:我正在尝试循环并增加以下内容:

var result_types = document.querySelectorAll('[data-title]')[0].attributes[2].nodeValue

specifically to grab and increment this value:专门用来获取和增加这个值:

[0].attributes

Currently, I have the following:目前,我有以下内容:

var card = document.querySelectorAll('[data-title]')[0].attributes[2].nodeValue;
  for (var i = 0; i < card.length; i++) {
  console.log(card[i]);
  }

I am trying to get this [0].attributes to increment to [1].attributes etc. when it is clicked我试图让这个 [0].attributes 在单击时增加到 [1].attributes 等

I am not sure what you are asking here, but if the issue is looping through the elements, this is happening because you get a NodeList back from querySelectorAll and not an array.我不确定你在这里问什么,但如果问题是循环遍历元素,这是因为你从querySelectorAll得到一个NodeList而不是一个数组。 Below will let you loop through node elements.下面将让您循环遍历节点元素。

const nodes = document.querySelectorAll('.nodes');

[].forEach.call(nodes, (singleNode) => {
    //Whatever you want.
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM