[英]Determine if a UL has 1 or more LI within
如何使用JavaScript確定UL中是否包含1個或多個LI?
偽代碼
if('ul#items' has >= 1 LI){
謝謝,
使用jQuery:
$('ul#items li').length >= 1
沒有jQuery:
document.getElementById('items').getElementsByTagName('li').length >= 1
使用jQuery:
if( $('#items li').length >= 1 ){...
使用document.getElementById("items").childNodes.length
和一個數字比較運算符。 如果ul
確實包含li
以外的其他節點,則必須對其進行過濾。
在jQuery中:
$("#items").children("li").length
我猜您只想要直接的孩子,所以不要使用find()。
if ($('ul#items').children('li').length) {
// etc.
}
您可以使用:
$('ul#items li').length
如果您使用的是jQuery
if($('ul > li').size()>0) {
}
這將檢查UL元素是否具有多於0個直接子li元素。
if ($('ul#items > li').length >= 1)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.