本文介绍了querySelectorAll 不起作用,但 querySelector 起作用了的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
为 chrome 制作简单的扩展.将querySelector"更改为querySelectorAll"时,第二个函数不起作用.
Making simple extension for chrome.Second function didn't work when changing "querySelector" into "querySelectorAll".
var ele2 = document.querySelectorAll(".view-count ");
window.onload = function(){
func2();
}
function func2(){
ele2.innerHTML = ele2.innerHTML.trim();
ele2.innerHTML = ele2.innerHTML.slice(0, -14);
ele2.textContent = "$" + ele2.innerHTML.replace(/ /g,' ');
}
解决方案:
for (x=0;x<ele2.length;x++){ele2[x].innerHTML = ele2[x].innerHTML.trim();}
for (x=0;x<ele2.length;x++){ele2[x].innerHTML = ele2[x].innerHTML.slice(0, -14);}
for (x=0;x<ele2.length;x++){ele2[x].textContent = "$" + ele2[x].innerHTML.replace(/ /g,' ');}
推荐答案
querySelectorAll
返回节点集合,而不是 querySelector
返回单个节点.
要访问集合中的节点,您必须通过索引引用它.
querySelectorAll
returns a collection of nodes as opposed to querySelector
which returns a single node.
To get to a node in the collection you'll have to reference it via an index.
ele2[0].innerHTML = ele2[0].innerHTML.trim();
这篇关于querySelectorAll 不起作用,但 querySelector 起作用了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!