Javascript 确定特定div在dom中是否存在任何子级
如何确定某个特定的div在dom中是否存在子级Javascript 确定特定div在dom中是否存在任何子级,javascript,jquery,css,Javascript,Jquery,Css,如何确定某个特定的div在dom中是否存在子级 <div class="profile-feed row"> <div class="col-sm-6"> </div> // does this particular div has any child? ... </div> //这个部门有孩子吗? ... 使用此 var count = $(".col-sm-6").children().length; 如果这个类有多个元素,这个方法
<div class="profile-feed row">
<div class="col-sm-6"> </div> // does this particular div has any child?
...
</div>
//这个部门有孩子吗?
...
使用此
var count = $(".col-sm-6").children().length;
如果这个类有多个元素,这个方法将返回这个id的所有元素的所有子元素的数目。更具体地说,我建议使用id选择器或添加父选择器。请参见JSFIDLE上的这个示例,您可以检查子元素的长度
var len = $(".col-sm-6").children().length;
if(len > 0)
alert('Child exist');
else
alert('Child does not exist');
您可以使用jQuery的
children()
更多信息请点击此处:这是一个正在工作的plunker:
$(文档).ready(函数()
{
if($('.col-sm-6').children().length>0)$('.status').text('div has child');
else$('.status').text('no child');
});
只需使用jquerychildren->returns数组,然后使用本机JS获取长度:)
var profile = document.querySelectorAll('.profile-feed .col-sm-6');
var childs = profile[0].querySelectorAll('*');
var kids = $('.col-sm-6').children();
if(kids.length > 0) {
alert('has children')
}
<script>
$(document).ready(function()
{
if($('.col-sm-6').children().length > 0) $('.status').text('div has child');
else $('.status').text('no child');
});
</script>
var profile = document.querySelectorAll('.profile-feed .col-sm-6');
var childs = profile[0].querySelectorAll('*');