繁体   English   中英

为什么!document.querySelectorAll不适用于多个ID

[英]Why !document.querySelectorAll not working for multiple id

我目前有这个工作,但是我需要添加一个附加元素,所以我切换到SelectorAll,但是它不起作用

这工作

if(!document.getElementById('body_options_45')) {
//Do Stuff
}

当我添加第二个ID并切换到selectorALl时,它停止工作

if(!document.querySelectorAll('#body_options_45, #body_options_113')) {
   //Do Stuff
}

我在这里想念什么?

querySelectorAll返回一个NodeList 您将需要像这样检查lengthif (!document.querySelectorAll('#body_options_45, #body_options_113').length)

querySelectorAll()返回一个NodeList NodeList不是数组,但是在这种情况下,行为是相同的:空数组和空NodeLists都是true

![] //returns false
!emptyNodeList //returns false

要检查您的NodeList是否为空,请使用length属性:

if(document.querySelectorAll('#body_options_45, #body_options_113').length===0) {
   //do stuff
}

暂无
暂无

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

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