javascript - 使用 Javascript 的 div 布局

标签 javascript jquery css jquery-ui

使用 Javascript 的 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>

脚本.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();
}

});


        });

请提出可以帮助我解决问题的想法。

最佳答案

不要硬编码 div 的选择器。这样您就可以将其扩展到单个 div 之外。

使用这样的东西:

$('button').on('click',function(){
    $(this).parent('div').css({'width' : '50%'});
});

Example

关于javascript - 使用 Javascript 的 div 布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11027655/

相关文章:

javascript - 对服务发出 http 请求,在 Controller 中获取响应

javascript - 如何在 Chrome 中定义 SharedArrayBuffer?

javascript - 使用来自 javascript 的液体 'include' 标签

css - 修复浏览器缩放时的图标大小(使用 Chrome)

javascript - 如何在 Grunt 中使用 stylelint?

javascript - 如何将 div 元素移动到另一个 div 元素内?

javascript - 从 React-Select 组件获取所选值的最佳方法是什么?

jQuery - 如何在 jQuery ajax 中传递多个回调函数

javascript - 无法通过使用jquery传递参数来调用webservice

javascript - jQuery.data() 适用于 Mac OS WebKit,但不适用于 iPhone OS?