jQuery,如果div ID有孩子 [英] jquery if div id has children
本文介绍了jQuery,如果div ID有孩子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这种if
条件正在给我带来麻烦:
This if
-condition is what's giving me trouble:
if (div id=myfav has children) {
do something
} else {
do something else
}
我尝试了以下所有操作:
I tried all the following:
if ( $('#myfav:hasChildren') ) { do something }
if ( $('#myfav').children() ) { do something }
if ( $('#myfav:empty') ) { do something }
if ( $('#myfav:not(:has(*))') ) { do something }
推荐答案
if ( $('#myfav').children().length > 0 ) {
// do something
}
这应该有效. children()
函数返回一个包含子级的JQuery对象.因此,您只需要检查一下大小,看看它是否至少有一个孩子.
This should work. The children()
function returns a JQuery object that contains the children. So you just need to check the size and see if it has at least one child.
这篇关于jQuery,如果div ID有孩子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文