jquery: if ul is empty

喜夏-厌秋 提交于 2019-11-29 03:06:27
if ($('#mylist li').length == 0) ...

I like using the children() method because it reads nicely and it works even if you've already cached the selector for your ul. Here's what it looks like:

$myList = $('#myList')
if ( $myList.children().length === 0 ) ...

jQuery always returns an array of elements. If no matches were found, the array will be empty. An empty array in Javascript evaluates to true:

console.log( !![] ); // logs: true

You want to check the length of the returned set:

if ( ! $("#mylist li").length ){
   $('#popuperrors').hide();
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!