簡體   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