隐藏嵌套的UL [英] hiding nested ULs

查看:98
本文介绍了隐藏嵌套的UL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好。


示例代码在这里 http://instaltech.com.pl/_drzewo/test.html 。我想要创建一个具有< li>的函数。 object(id =" myLI" in example

code)作为参数传递并递归隐藏(css display:none)

所有嵌套在下面的UL都通过了LI。无论这些UL是否嵌套在LI

作为参数传递,或者在其子女或其子女等等中传递。

有人能帮助我吗?我有这个递归的问题。或许

递归不是他最好的解决方案吗?


祝你好运,

ABS

解决方案

如果你的< li>只会有< span>和其他< ul>作为孩子一样,你/ b $ b可能仅用于(;;)通过它们并隐藏除第一个孩子以外的所有孩子

(记住换行符是节点在Mozilla中,但不是在IE中,所以

你的家庭大小会有所不同)


如果你想做的话就不需要妄想让孩子们(等人)

看不见。让孙子独自一人,隐藏直系后代。

孙子孙女也会消失。


一个循环,你就完成了。


如果您只想隐藏<无序列表>,则需要在< list item>的所有子项中运行

a循环。并且只隐藏那些具有nodeName''UL''的
孩子:


myElement = document.getElementById(''myLI'');

for(var c = 0; c< myElement.childNodes.length; c ++)

myElement.childNodes [c] .nodeName ==''UL''&& (

myElement.childNodes [c] .style.display =''none''

);


如果你想要通过并明确隐藏所有< ul> s,无论

他们离祖先有多远,递归就是通过

去的方式。


函数hideAllUL(element){

for(var c = 0; c< element.childNodes.length; c ++){

element.childNodes [c] .nodeName ==''UL''&& (

element.childNodes [c] .style.display =''none''

);


hideAllUL(element .childNodes [c]);

}


返回true;

}


Random写道:

如果您的< li>只会有< span>和其他< ul>作为孩子,
你可能只是(;;)通过它们并隐藏除第一个孩子以外的所有孩子
(请记住,换行符是Mozilla中的节点,但不是IE中的节点,所以
你的家庭人数会有所不同)

如果你想要做的就是让孩子们(看不见),就不需要妄想。让孙子独自一人,隐藏直接的后代。孙子孙女也会消失。

一个循环就完成了。


我理解,但我希望隐藏儿童的< ul> s,当我将b / b
显示回来时。

如果你想通过并明确隐藏所有< ul> s,无论它们与祖先相距多远,递归都是通往
的方式。 [...]




是的,这就是我所需要的。非常感谢你给我指路:)


祝你好运,

ABS

Random写道:

如果你的< li>只会有< span>和其他< ul>作为孩子,
你可能只是(;;)通过它们并隐藏除第一个孩子以外的所有孩子
(请记住,换行符是Mozilla中的节点,但不是IE中的节点,所以
你的家庭人数会有所不同)

如果你想要做的就是让孩子们(看不见),就不需要妄想。让孙子独自一人,隐藏直接的后代。孙子孙女也会消失。

一个循环就完成了。


我理解,但是当我要将它们显示回来时,我希望隐藏< li> s的孩子的所有< ul>是隐藏的,无论它们有多深。

如果你想通过并明确隐藏所有< ul> s,无论它们与祖先的距离有多远,递归就是去的方式。 [...]




是的,这就是我所需要的。非常感谢你给我指路:)


祝你好运,

ABS



Hi all.

The example code is here http://instaltech.com.pl/_drzewo/test.html. I''d
like to create a function which will have <li> object (id="myLI" in example
code) passed as a parameter and will recursively hide (css display: none)
all ULs nested below passed LI. No matter if such ULs are nested in LI
passed as a parameter or in its children or childer of its children etc.
Could anybody help me ? I''ve got problems with that recursivelity. Or maybe
the recursivelity is not he best solution for this ?

Best regards,
ABS

解决方案

If your <li> will only have the <span> and other <ul>s as children, you
might just for(;;) through them and hide all but the first child
(remember that line breaks are nodes in Mozilla, but not in IE, so
you''ll have a difference in family size)

No need for recusion if all you want to do is make the children (et al)
unseen. Leave the grandchildren alone and hide the direct descendants.
The grandkids will disappear too.

One loop and you''re done.

If you strictly want to hide only <unordered lists>, you''ll need to run
a loop through all children of your <list item> and hide only those
children with the nodeName ''UL'':

myElement = document.getElementById( ''myLI'' );
for( var c = 0; c < myElement.childNodes.length; c++ )
myElement.childNodes[c].nodeName == ''UL'' && (
myElement.childNodes[c].style.display = ''none''
);

If you want to go through and explicitly hide all <ul>s, regardless of
how far removed they are from their ancestor, recursion is the way to
go.

function hideAllUL( element ) {
for( var c = 0; c < element.childNodes.length; c++ ) {
element.childNodes[c].nodeName == ''UL'' && (
element.childNodes[c].style.display = ''none''
);

hideAllUL( element.childNodes[ c ] );
}

return true;
}


Random wrote:

If your <li> will only have the <span> and other <ul>s as children,
you might just for(;;) through them and hide all but the first child
(remember that line breaks are nodes in Mozilla, but not in IE, so
you''ll have a difference in family size)

No need for recusion if all you want to do is make the children (et
al) unseen. Leave the grandchildren alone and hide the direct
descendants. The grandkids will disappear too.

One loop and you''re done.
I understand but I want the <ul>s which are children to be hidden when I
will show them back.
If you want to go through and explicitly hide all <ul>s, regardless of
how far removed they are from their ancestor, recursion is the way to
go. [...]



Yes, that''s what I needed. Thank you so much for showing me the way :)

Best regards,
ABS


Random wrote:

If your <li> will only have the <span> and other <ul>s as children,
you might just for(;;) through them and hide all but the first child
(remember that line breaks are nodes in Mozilla, but not in IE, so
you''ll have a difference in family size)

No need for recusion if all you want to do is make the children (et
al) unseen. Leave the grandchildren alone and hide the direct
descendants. The grandkids will disappear too.

One loop and you''re done.
I understand but I want all the <ul>s which are children of <li>s to be
hidden when I will show them back, no matter how deep they are.
If you want to go through and explicitly hide all <ul>s, regardless of
how far removed they are from their ancestor, recursion is the way to
go. [...]



Yes, that''s what I needed. Thank you so much for showing me the way :)

Best regards,
ABS




这篇关于隐藏嵌套的UL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆