js控制div排序
例:页面代码
<script type="text/javascript">
function show1(){
$("#test").show();
var arr = ['5','3','2','1','4'];
for(var i=0;i<arr.length;i++){
$("#"+arr[i]).show();
}
}
function show2(){
$("#test").show();
var arr = ['3','4','2'];
for(var i=0;i<arr.length;i++){
$("#"+arr[i]).show();
}
}
</script>
<style type="text/css">
div{
border: 1px solid #ccc;
display: none;
}
</style>
</head>
<body>
<input type="button" value="显示1" onclick="show1()">
<input type="button" value="显示2" onclick="show2()">
<div id="test" style="width:400px;">
<div id="1">1</div>
<div id="2">2</div>
<div id="3">3</div>
<div id="4">4</div>
<div id="5">5</div>
</div>
</body>
以上效果始终:
<div>1</div><div>2</div><div>3</div><div>4</div><div>5</div>
想达到的
效果1:<div>5</div> <div>3</div><div>2</div><div>1</div><div>4</div>
效果2:<div>3</div> <div>4</div><div>2</div>
js?dvi?
div
function
------解决方案--------------------
function show1(){
$("#test").show();
$('#test div').hide();
var arr = ['5','3','2','1','4'];
for(var i=0;i<arr.length;i++){
$("#"+arr[i]).appendTo($("#test")).show();
}
}
function show2(){
$("#test").show();
$('#test div').hide();
var arr = ['3','4','2'];
for(var i=0;i<arr.length;i++){
$("#"+arr[i]).appendTo($("#test")).show();
}
}
</script>