<div class="list">
<div>div1 </div>
<div>div2 </div>
<div>div3 </div>
<div>div4 </div>
</div>我如何获取 class =list 下面的所有DIV
然后根据不同索引来show 和hide例如我有个涵数传进来的索引是 2 那么则显示DIV2 内容,其它 隐藏掉,
我的心路是先获取下面的所有DIV, 就是这个不会取, 然后做循环来判断索引, 求高人指点..
<div>div1 </div>
<div>div2 </div>
<div>div3 </div>
<div>div4 </div>
</div>我如何获取 class =list 下面的所有DIV
然后根据不同索引来show 和hide例如我有个涵数传进来的索引是 2 那么则显示DIV2 内容,其它 隐藏掉,
我的心路是先获取下面的所有DIV, 就是这个不会取, 然后做循环来判断索引, 求高人指点..
$("div.list").css("display", "none");//or $("div.list").hide();
show(2);//显示第二个
});
function show(index) {
$("div.list div:eq(" + (index + 1) + ")").show();
}
var i = 0;
$("div.list").each(function(this) {
i++;
if(i==2)
$(this).css('display','');
else
$(this).css('display','none');
});
});
//注册相关事件
$(function () {
$("div.list").css("display", "none");//or $("div.list").hide();
show(2);//显示第二个$("#baobiao-list ul li:eq(0) a").click(function(){
show(1);
});
$("#baobiao-list ul li:eq(1) a").click(function(){
show(2);
});
});
function show(index) { $("div.list div:eq(" + (index + 1) + ")").show();
}
$(".list div").each(function(){
$(this).hide();
}).end().eq(2).show();//索引为2的话是第三个DIV
///或者end()去掉换成
$(".list div").eq(2).show();
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
<script type= "text/javascript" src= "jquery-1.2.6.js"> </script>
<script type= "text/javascript">
$(function(){
$("#show").click(function(){
var hvalue=$("#hv").val();
hvalue=hvalue-1;
$("div.list div:eq("+hvalue+")").show().siblings().hide();
});
})
</script>
<style type="text/css">
.list div {height:100;width:100;color:red;background:blue;margin:5;}
</style>
</HEAD> <BODY>
<div class="list">
<div>海上生明月,</div>
<div>天涯共此时。</div>
<div>情人怨遥夜,</div>
<div>竟夕起相思。</div>
</div>
<input type="text" id="hv"/>
<input type="button" id="show" value="ShowDIV"/>
</BODY>
</HTML>
$(".list").childen("div")//取下面所有div