简体   繁体   English

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

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

I currently have this working , but i need to add an additional element so i switched to SelectorAll , but it doesn't work 我目前有这个工作,但是我需要添加一个附加元素,所以我切换到SelectorAll,但是它不起作用

This is working 这工作

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

When i add a 2nd ID and switch to selectorALl , it stops working 当我添加第二个ID并切换到selectorALl时,它停止工作

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

What am i missing here ? 我在这里想念什么?

querySelectorAll returns a NodeList . querySelectorAll返回一个NodeList You will need to check the length like this: if (!document.querySelectorAll('#body_options_45, #body_options_113').length) 您将需要像这样检查lengthif (!document.querySelectorAll('#body_options_45, #body_options_113').length)

querySelectorAll() returns a NodeList . querySelectorAll()返回一个NodeList NodeLists are not arrays but, in this particular case, the behavour is the same: empty arrays and empty NodeLists are both truthy . NodeList不是数组,但是在这种情况下,行为是相同的:空数组和空NodeLists都是true

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

To check if your NodeList is empty or not, use the length property: 要检查您的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