您的位置:首页 > Web前端 > JQuery

使用原生js和jquery 对 ul 中的li进行随机排序

2017-06-23 14:38 471 查看
html代码:

<ul id="foo">
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
</ul>

javascript代码:

var ulEle = document.getElementById("foo");
var liList = ulEle.getElementsByTagName("li");
var leng = liList.length;
var lis = Array.prototype.slice.call(liList, 0);
var idx = leng;
while(idx-- > 0)
ulEle.insertBefore(lis[idx],lis[parseInt(Math.random() * lis.length)])


jquery代码:

$("#foo li").each(function(){
if(parseInt(Math.random()*2)==0){
$(this).prependTo($(this).parent());
}
});
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: