如何在循环期间隐藏和检查无序列表中每个li的另一个元素的属性?

时间:2012-12-12 20:00:17

标签: javascript jquery

好的 - 我试图通过无序列表中包含的<li>反向循环。在此循环期间,我想删除每个<li>,然后将其父列的高度与相邻列的高度进行比较,直到它为=&lt;另一个。一旦这是真的,打破循环并显示剩余的<li>。在此之前我正在使用shuffle.js来洗牌。我已经能够捕获两列的高度,但是一旦我开始循环遍历<li>

,我的代码似乎就搞砸了

这是HTML简短版本......

<body>  
  <div id="wrap">
        <div id="header"></div>
             <div id="main">This is the column that would set the height that determines the amount of [li] shown
            </div>
        <div id="sidebar">
            <ul id="shuffleunorderedlist">
                <li id="promo_1">
                    1
                </li>
                <li id="promo_2">
                    2
                </li>
                <li id="promo_3">
                    3
                </li>
                <li id="promo_4">
                    4
                </li>
                <li id="promo_5">
                    5
                </li>
                <li id="promo_6">
                    6
                </li>
                <li id="promo_7">
                    7
                </li>
                <li id="promo_8">
                    8
                </li>
                <li id="promo_9">
                    9
                </li>
                <li id="promo_10">
                    10
                </li>
                <li id="promo_11">
                    11
                </li>
                <li id="promo_12">
                    12
                </li>

            </ul>
        </div>
<div id="footer"></div>

        

这是我到目前为止所写的jQuery和Javascript。

<script type="text/javascript">
jQuery(function($)
{
    window.onload = function()
    { 
        $('#shuffleunorderedlist').shuffle();   //********** Shuffle List 
    };
    var mainHeight = $('#main').height();   //********** Capture 'main' height 
    var sidebarHeight = $('#sidebar').height();     //********** Capture 'sidebar'     height 
        if (mainHeight > sidebarHeight)     //********** Compare 'sidebar' height 
            {
                var liCheck = $('div#sidebar.li').reverse().each(function ()    //********** reverse Loop through <li>'s  
                    {
                    while(liCheck())
                        {
                           $('li').hide(); //********** reverse Loop through <li>'s 
                             if (sidebarHeight =< mainHeight) 
                                    {
                                    break;  // breaks loop completely   //**********  Output <li>'s that are left 
                                    }
                        }  
                    }   
            }               
});
</script>

所以基本上为什么这不起作用?我在哪里打破它?我怎样才能让它发挥作用?

非常感谢任何建议,帮助和答案。

1 个答案:

答案 0 :(得分:0)

您的代码充满了问题,因此请将代码与此进行比较,而不是详细说明:

var mainHeight = $('#main').height();
var sidebarHeight = $('#sidebar').height();
if (mainHeight < sidebarHeight) { // You are hiding things from the sidebar correct? Strictly speaking, this check isn't necessary, but it prevents us from looping when we don't need to. 
    $('div#sidebar li').each(function() {  // I removed reverse as it gave me a reference error. If it is defined for you, feel free to use it. 
        if (sidebarHeight > mainHeight) {  
            $(this).hide();  // hide only the current element
            sidebarHeight = $("#sidebar").height(); // update sidebarHeight 
        }
    });
}