![](/img/trans.png)
[英]jQuery add class to children of a tag and children of children of all the tags
[英]Jquery : Hide all children, and their children and etc… of a tag
我正在使用DOM(javascript + jquery)制作游戏,并且需要切换阶段,为此我需要将所有阶段隐藏起来,并显示所有新阶段。 显示部分很容易,因为我正在创建新的东西。 但是我想要一种干净的方法来查找div的每个最后一个孩子,他的所有后代,堂兄,侄女(我不在乎)并将其隐藏。 为了在代码中显示类似:
while (child = $(this).hasChildren)
{
child.hide();
}
而不是像这样的东西:
$(this.id).children().children().children().hide();
$(this.id).children().children().hide();
$(this.id).children().hide()
$(this.id).hide();
目的是隐藏他的孩子以及他的孩子的孩子,等等。 我希望我不要太困惑。 预先感谢您的回答!
编辑:对于不想隐藏孩子但可以访问所有孩子的人,您不能使用.find(“ *”),那么第二个答案可能更适合您(来自Rajan Goswami的那个)。
如果要对元素的所有子元素执行某些操作,则可以使用以下语法:
$('#myDiv *').hide();
或使用.find()
$('#myDiv').find('*').hide();
当然,您可以使用任何功能,而不仅仅是.hide()
您可以尝试以下解决方案:
“ myParentControl”是最父控件的ID。
$(document).ready(function(){
HideChildren($(#myParentControl))
})
function HideChildren(cntrl){
if ( $(cntrl).children().length > 0 ) {
$(cntrl).children().each(function(){
HideChildren(this);
$(this).hide();
})
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.