
Originally Posted by
youlichika
em... you are right, for my careless, this is not use Math random(), it just random the div orders. maybe I shall still looking for the correct one.
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Randomise position of specified existing elements of a common parent</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" >
<style type='text/css'>
div{border:solid #f00 1px; width:30%}
</style>
</head>
<body>
<div id="div1">FIRST</div>
<div id="div2">SECOND</div>
<div id="div3">THIRD</div>
<script type='text/javascript'>
function orderElems()
{
var elems = [], tempElem, pn, args = orderElems.arguments;
for( var i = 0, len = args.length; i < len; i++ )
( pn = ( elems[ i ] = tempElem = document.getElementById( args[ i ] ) ).parentNode).removeChild( tempElem );
for( var i = len - 1, idx, tmp; i > -1; i-- )
{
while( ( idx = Math.floor( Math.random() * len ) ) == i )
;
tmp = elems[ i ];
elems[ i ] = elems[ idx ];
elems[ idx ] = tmp;
}
for( var i = 0; i < len; i++ )
pn.appendChild( elems[ i ] );
}
orderElems( 'div1', 'div2', 'div3' );
</script>
</body>
</html>
Bookmarks