且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

JS 刷新随机统制DIV的的位置

更新时间:2022-08-13 09:41:00

<script type="text/javascript"> 

window.onload = function() 

var box = document.getElementById('box_0531'); 

var obj = box.getElementsByTagName('div'), ar = []; 

//var obj = box.getElementsByClassName('cont'), ar = []; 获取所有指定class名字

for (var i = 0; i < obj.length; i ++) 

ar.push(obj[i]); 

function sortRandom(a, b) { 

return Math.random() - 0.5; 

ar.sort(sortRandom); 

box.innerHTML = ''; 

for (var i = 0; i < ar.length; i ++) 

box.appendChild(ar[i]); 

</script>

</head><body>

<div class="box_0531" id="box_0531">

  <div class="cont" id="cont1">CONT1</div>

  <div class="cont" id="cont2">CONT2</div>

  <div class="cont" id="cont3">CONT3</div>

  <div class="cont" id="cont4">CONT4</div>

  <div>CONT5</div>

  <div>CONT6</div>

</div>



本文转自 gutaotao1989 51CTO博客,原文链接:http://blog.51cto.com/taoyouth/1750923