【问题标题】:Displaying random li's display's NULL显示随机 li 的显示 NULL
【发布时间】:2011-03-25 09:26:17
【问题描述】:

如何显示一组随机列表 项目,我想我有这个了。 谢谢!

//统计对象中li的个数 var listCount = $("li.contentBlock", obj).length;

        //Generate a random number from the count of li's
        var randomListNumber = Math.floor(Math.random() * listCount -1);

        //Generate list of items based on the amount of li's that are present
        var firstList = "<li class='contentBlock'>"+$("li:eq("+randomListNumber+")", obj).html()+"</li>";

        //Target element that contains that list
        var place = $("ul", obj).html();

        //Combine ul container and list generate
        var newPlace = firstList+place;

        //Replace current ul and li with newly generated random list
        $("ul", obj).html(newPlace);

【问题讨论】:

  • 在加载页面时,我试图在 ul 中随机生成列表项。 obj 是此内容库的 var。

标签: jquery random null


【解决方案1】:

这样的?将其保存为 .html 文件作为示例

<html>
<head>
    <script src="http://code.jquery.com/jquery-1.4.2.min.js"></script>
    <script>
        $(document).ready(function(){
            var len = $("ul.contentBlock li").length;       

            $("ul.contentBlock li").each(function(){
                $(this).html(Math.floor(Math.random() * len));
            });

        });
    </script>
</head>
<body>
    <ul class="contentBlock"> 
        <li></li>
        <li></li>
        <li></li>
        <li></li>
    </ul>
</body>
</html>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-01
    相关资源
    最近更新 更多