js随机排序问题
发布网友
发布时间:2022-04-23 04:13
我来回答
共1个回答
热心网友
时间:2022-04-23 05:42
<html>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<body>
<div class="haha"><ul>
<li>苹果</li><li>桔子</li><li>香蕉</li><li>石榴</li><li>桃子</li><li>菠萝</li>
</ul>
<div class="mama"><ul>
<li>苹果</li><li>桔子</li><li>香蕉</li><li>石榴</li><li>桃子</li><li>菠萝</li>
</ul>
<script type="text/javascript">
var $container = document.getElementsByTagName('ul')[1],
$frag = document.createDocumentFragment(),
$children = Array.prototype.slice.call($container.children),
$child,
rnd = 0,
total = 7;
while(--total){
rnd = Math.floor(Math.random()*total - 1);
$child = $children.splice(rnd, 1)[0];
console && console.log($child.innerHTML);
$frag.appendChild($child);
}
$container.appendChild($frag);
</script>
</body>
</html>