使用Java的Div布局
如果我单击+它应该是打开的,然后再次单击-它应该是关闭的,它工作正常,但是我需要的是,如果还有更多的div如何在运行时处理它们。
<html>
<head>
<script src="jquery-1.7.1.js"></script>
<script src="script.js"></script>
<style type="text/css">
#widnow{
width:100%;
border:solid 1px;
float:left;
}
#title_bar{
background: #FEFEFE;
height: 25px;
width: 100%;
}
#button{
border:solid 1px;
width: 25px;
height: 23px;
float:right;
cursor:pointer;
}
.box{
height: 50%;
width: 50%;
background: #DFDFDF;
float:left;
}
#title_bar1{
background: #FEFEFE;
height: 25px;
width: 100%;
}
#button1{
border:solid 1px;
width: 25px;
height: 23px;
float:right;
cursor:pointer;
}
.box1{
height: 50%;
width: 50%;
background:#C0C0C0;
float:left;
}
</style>
</head>
<body>
<div id="widnow">
<div class="box" >
<div id="title_bar">
<div id="button">+</div>
</div>
</div>
<div class="box1">
<div id="title_bar1">
<div id="button1">-</div>
</div>
</div>
</div>
</body>
</html>
script.js
jQuery().ready(function() {
$("#button").click(function(){
if($(this).html() == "-"){
$(this).html("+");
$( ".box" ).css( "width","50%" );
$( ".box1" ).show();
}
else{
$(this).html("-");
$( ".box" ).css( "width","100%" );
$( ".box1" ).hide();
}
});
});
请提出可以帮助我解决问题的想法。
最佳答案
使用相对路径/例如
$(this).parent().css("width","50%");
因此,您始终对包含已单击的(+/-)元素的框进行样式设置。
您还需要修改$(“。box1”)显示/隐藏代码;正如Pulkit所说-您应该使用类而不是ID。 (选择父类下面具有类(“ box1”)的子类是:
$(this).parent().children("#box1")
关于javascript - 使用Java的Div布局,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11027655/