JavaScript原型问题

发布于 2024-08-24 01:30:46 字数 883 浏览 5 评论 0原文

所以我有一个相当基本的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

七婞 2024-08-31 01:30:47

这是 jQuery 解决方案(原型必须类似):

$('div.alist').css('display', function () {
        return $(this).find('li:visible').length < 1 ? 'none' : '';
    });

This is jQuery solution (Prototype must be something similar):

$('div.alist').css('display', function () {
        return $(this).find('li:visible').length < 1 ? 'none' : '';
    });
执笔绘流年 2024-08-31 01:30:46

像这样的事情可能会让你开始。您需要遍历子级并检查它们是否可见。如果其中任何一个不是,则设置一个标志并从循环中中断。如果该标志为 false,则无需隐藏 div

function sweep(){
    $('div.alist').each( function( s ) {
        var shouldHide = true;
        var children = s.down().childElements();
        children.each(function(li) {
            if(li.visible()) {
                shouldHide = false;
                throw $break;
            }
        });

        if(shouldHide) {
            s.hide();
        }
    }); 
} 

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.

function sweep(){
    $('div.alist').each( function( s ) {
        var shouldHide = true;
        var children = s.down().childElements();
        children.each(function(li) {
            if(li.visible()) {
                shouldHide = false;
                throw $break;
            }
        });

        if(shouldHide) {
            s.hide();
        }
    }); 
} 
怀里藏娇 2024-08-31 01:30:46

您可以使用 Element< 的 select() 方法/code> 查找所有 li 后代。并为每个 li< 运行一个方法 Array.all /code> 并检查是否全部返回 true。如果全部返回 true 则隐藏 div。

function sweep() {
    // check each div
    $('div.alist').each(function(element) {
        var listItems = element.select('li');
        // are the list items of this div hidden?
        var listItemsHidden = listItems.all(function(item) {
            return item.visible();
        });
        // hide the div too if so
        if(listIemsHidden) {
            element.hide();
        }
    });
}

该代码未经测试。

You could use the select() method of Element to find all li descendants. And run a method Array.all for each li and check if all return true. Hide the div if all return true.

function sweep() {
    // check each div
    $('div.alist').each(function(element) {
        var listItems = element.select('li');
        // are the list items of this div hidden?
        var listItemsHidden = listItems.all(function(item) {
            return item.visible();
        });
        // hide the div too if so
        if(listIemsHidden) {
            element.hide();
        }
    });
}

This code is untested.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文