我正在尝试查找所有输入元素,并根据我想要搜索 id 以匹配特定关键字的元素的 id。如果找到匹配项,我想将元素放入数组中targetNodse
。该变量nodes
包含所有输入,当我记录我得到的数组时,[-1, -1, 0, 0]
它告诉我它找到了带有关键字的特定输入元素survey-answer
。但是我如何访问元素属性,而不仅仅是告诉我它找到了它?
HTML
<input type="radio" id="poll-answer-8234" name="poll_430" value="8234">
<input type="radio" id="poll-answer-9234" name="poll_430" value="9234">
<input type="radio" id="survey-answer-7866" name="poll_430" value="7866">
<input type="radio" id="survey-answer-8998" name="poll_430" value="8998">
JS
var targetNodes, nodes, count;
targetNodes = [];
nodes = document.getElementsByTagName("input");
for (count = 0; count < nodes.length; count++) {
node = nodes[count].id.indexOf("survey-answer");
targetNodes.push(node);
}
console.log(targetNodes);