是否可以通过document.querySelector()获得多个ID? [英] Is it possible to get multiple ID's with document.querySelector()?
本文介绍了是否可以通过document.querySelector()获得多个ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以使用document.querySelector()查找多个ID(例如div1,div2,div3)?
Is it possible to find more than one ID (e.g. div1, div2, div3) by using document.querySelector()?
如果没有,那么还有替代解决方案吗?
If not then is there a alternative solution?
推荐答案
是的,您可以这样做。首先,为了检索多个元素,您必须使用 querySelectorAll
。其次,您只需用逗号分隔所有ID,就和CSS一样。
Yes, you can by do that. First of all, in order to retrieve multiple elements, you'll have to use querySelectorAll
. Second, you just separate all the ids with commas, it's no different from CSS.
var nodes = document.querySelectorAll('#a, #b, #c');
这将为您提供所有 NodeList
匹配ID的元素。
This will give you a NodeList
of all the elements matching the IDs.
这篇关于是否可以通过document.querySelector()获得多个ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文