Displaying random li's display's NULL
How do I display a random set of list items, I think I have this down. Thanks!
//Count the number of li's in the object 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;
//Re开发者_开发百科place current ul and li with newly generated random list
$("ul", obj).html(newPlace);
Something like this? Save this as an .html file for an example
<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>
精彩评论