【发布时间】: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。