JavaScript原型问题
所以我有一个相当基本的javascript问题,我已经把头撞到墙上有一段时间了:
<div class='alist'>
<ul>
<li class='group_1'> An Item </li>
<li class='group_1'> An Item </li>
<li class='group_2'> An Item </li>
</ul>
</div>
<div class='alist'>
<ul>
<li class='group_1'> An Item </li>
<li class='group_1'> An Item </li>
<li class='group_2'> An Item </li>
</ul>
</div>
<script>
function toggle_item( num ){
$$( 'li.group_' + num ).invoke('toggle');
}
</script>
基本上,我需要创建一个清扫器,将div设置为display:none,如果所有li都是display:none。
我认为它会这样开始:
function sweep(){
$$('div.alist').each( function( s ) {
ar = s.down().children
});
}
任何关于好的教程的建议也将受到欢迎
So I have a rather basic javascript problem which I have been slamming my head into a wall over for awhile:
<div class='alist'>
<ul>
<li class='group_1'> An Item </li>
<li class='group_1'> An Item </li>
<li class='group_2'> An Item </li>
</ul>
</div>
<div class='alist'>
<ul>
<li class='group_1'> An Item </li>
<li class='group_1'> An Item </li>
<li class='group_2'> An Item </li>
</ul>
</div>
<script>
function toggle_item( num ){
$( 'li.group_' + num ).invoke('toggle');
}
</script>
Basically, I need to create a sweeper that sets the div to display:none if all the li are display:none.
I think it would start like:
function sweep(){
$('div.alist').each( function( s ) {
ar = s.down().children
});
}
Any suggestions for good tutorials would be welcome as well
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是 jQuery 解决方案(原型必须类似):
This is jQuery solution (Prototype must be something similar):
像这样的事情可能会让你开始。您需要遍历子级并检查它们是否可见。如果其中任何一个不是,则设置一个标志并从循环中中断。如果该标志为 false,则无需隐藏
div
。Something like this might get you started. You'll need to iterate through the children and check if they're visible. If any of them aren't, set a flag and break from the loop. If the flag is false then you don't need to hide the
div
.您可以使用
Element< 的 select() 方法/code> 查找所有
li
后代。并为每个li< 运行一个方法 Array.all /code> 并检查是否全部返回 true。如果全部返回 true 则隐藏 div。
该代码未经测试。
You could use the select() method of
Element
to find allli
descendants. And run a method Array.all for eachli
and check if all return true. Hide the div if all return true.This code is untested.