例如,我有一个div <div id="myrules"></div>
如果我用
alert($("#myrules").find("if"))它提醒[object object],即使div是空的,所以问题是如何判断子程序是否存在,是否使用.find()。
发布于 2013-12-07 04:43:07
jQuery返回一组匹配的元素,如果没有选择元素,则此集合为空,因此需要对照返回值的长度属性进行检查。
// "length" of the returned collection is 0
if(jQuery('#myelem').find('.idontexist').length)
alert("element found");也可以在这里找到:http://api.jquery.com/jQuery/
发布于 2013-12-07 04:43:38
您可以使用$('#myrules').find('if').length
发布于 2013-12-07 04:47:40
您可以检查以下元素的子元素:
HTML
<div id="myrules">
<div class="ch">1</div>
<div class="ch">2</div>
<p>3</p>
</div>jQuery:
alert($("#myrules").children().length) // Alerts total children exists此外,还可以在元素中检查特定的总子级:
alert($("#myrules").children('div').length)
alert($("#myrules").children('p').length)https://stackoverflow.com/questions/20437675
复制相似问题