gpt4 book ai didi

javascript - 按钮切换的正确算法

转载 作者:太空宇宙 更新时间:2023-11-04 03:31:16 25 4
gpt4 key购买 nike

我有两个按钮,Edit_1 和 Edit_2。通过点击它们中的每一个,一个“扩展”div 应该出现在被点击的按钮的正下方。

在我编写的函数中,如果“扩展”div 的显示属性在 edit_1 下为 'block',然后单击 edit_2,则 widow 将在 edit_2 下移动。但是如果我点击 edit_1 本身,“扩展”窗口不会消失。

我可以通过添加另一个“扩展”窗口轻松解决问题,但是随着“编辑”标签的增加,我需要在其中正确移动这个“一个”扩展窗口。如果您能帮助我,我将不胜感激;

HTML:

<div id="container">
<div id="section_1"></div>
<div id="section_2"></div>
<button id="edit_1" onClick="edit(1);"></button>
<button id="edit_2" onClick="edit(2);"></button>
<div id="expansion"></div>
</div>

CSS:

*{
margin:0px;
padding:0px;}

body {
width:100%;
background-color:#F4F4F2;
margin-top:15px;
font-family:verdana;}

#container{
width:820px;
height:400px;
margin-left:auto;
margin-right:auto;
margin-top:0px;
border: dashed 2px blue;
position:relative;
z-index:1;}

#section_1{
width:800px;
height:198px;
border-top: solid 2px #D24726;
background-color:#ffcccc;
top:0px;
position: absolute;
z-index:2;}

#section_2{
width:800px;
height:198px;
border-top: solid 2px #14826D;
background-color:#C1FBDE;
top:200px;
position: absolute;
z-index:2;}

#edit_1{
width:50px;
height:15px;
position:absolute;
margin-left:740px;
margin-top:15px;
border:none;
cursor:pointer;
z-index:4;
background:url(../images/edit.fw.png) no-repeat;}

#edit_2{
width:50px;
height:15px;
position:absolute;
margin-left:740px;
margin-top:215px;
border:none;
cursor:pointer;
z-index:4;
background:url(../images/edit.fw.png) no-repeat;}

#expansion{
width:200px;
height:120px;
background-color:#FFFFFF;
position:absolute;
z-index:3;
margin-left:600px;
top:0px;
padding-top: 40px;
padding-left:10px;
padding-right:10px;
border-top:solid 2px #D24726;
display:none;}

JavaScript:

function edit(clicked_edit){
var click=document.getElementById('expansion').style.display;
if (click=='block'){ /* in any case, if the display property is block, it turns it to none*/
document.getElementById('expansion').style.display='none';
}
var tp=document.getElementById('section_'+clicked_edit).offsetTop;
document.getElementById('expansion').style.top=tp+'px';
document.getElementById('expansion').style.display='block';
}

JSFiddle

最佳答案


JQuery 方法


您可能会发现这个 JSFiddle 很有用它使用了一个很好的切换效果。

JQuery 是:

jQuery(document).ready(function(){
jQuery('#hideshow').live('click', function(event) {
jQuery('#content').toggle('show');
});
});

我很确定你可以在你的元素中使用它:)


Javascript 方法


看看this one - 它没有使用 JQuery,应该适合你:)

找到了here :


另一种方法


this demo 也是另一种显示/隐藏 div 的方式。所以有很多选项可供选择! :)

<script>
function showhide()
{
var div = document.getElementById("newpost");
if (div.style.display !== "none") {
div.style.display = "none";
}
else {
div.style.display = "block";
}
}
</script>

关于javascript - 按钮切换的正确算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26277262/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com