querySelectorAll.style不起作用 [英] querySelectorAll.style does not work
本文介绍了querySelectorAll.style不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在JavaScript中编写了一些我需要使用 querySelectorAll.style
的东西,但它总是返回undefined,但它完全适用于 querySelector.style
。
I am writing something in JavaScript that I need to use querySelectorAll.style
but it always returns undefined, but it works perfectly with querySelector.style
. How can I make it work properly so I can set the style?
document.querySelector("div#tabs" + tabId + "> div.page").style.display = 'none'; //works
document.querySelectorAll("div#tabs" + tabId + "> div.page").style.display = 'none';// doesn't work
推荐答案
querySelectorAll 返回元素列表而不是单个元素。
querySelectorAll returns a list of elements rather than a single one.
所以这应该是将样式应用于找到的第一个元素:
So this should work to apply the style to the first element found:
document.querySelectorAll("div#tabs" + tabId + "> div.page")[0].style.display = 'none'; // First element
这篇关于querySelectorAll.style不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文