【发布时间】:2020-07-12 16:52:48
【问题描述】:
function myFunction() {
var element = document.getElementById("one1");
element.classList.toggle("one2");
var element = document.getElementById("two1");
element.classList.toggle("two2");
}
<style>
#section{margin-left:50px;
margin-right:50px
}
#monbouton{float:right;
font-size:25px;
width:50px;
height:50px;
background-color:#F1F1F1;
border:1px solid ##F1F1F1
}
#one1{
float:left;
width:40%;
height:100px;
border:1px solid blue
}
.one2{
width:10% !important;
height:200px;
border:1px solid red !important;
}
.one2 #afairedisparaitre{display:none
}
#two1{float:right;
width:59%;
height:100px;
border:1px solid green
}
.two2{width:89% !important
}
</style>
<!DOCTYPE html>
<html>
<body>
<div id="section">
<div id="one1">
<button id="monbouton" onclick="myFunction()">↔</button>
<div id="afairedisparaitre">This is DIV #one1<br />
Button toggle to CLASS .one2<br />
and reverse</div>
</div>
<div id="two1">This is DIV #two1<br />
Button toggle to CLASS .two2<br />
and reverse</div>
</div>
</body>
</html>
我正在尝试缩小我网站的左侧,以便用户觉得更舒适时可以拥有更宽的右侧。 我缺少的是一种方法,可以在加载其他页面时在整个站点上保留选择,直到用户再次单击。也许解决方案是在 "localStorage" 的 js 中多写几行?非常感谢任何帮助。
【问题讨论】:
-
对不起,我想我错误地删除了我的答案......我想热烈感谢你的回答。我试图实施你的建议,但它不起作用,我可能忘记了一些东西......请在这里找到一个链接,因为它需要 2 个页面来查看即使在加载新页面后它是否保持用户的选择lepointdufle.net/toggl1.htm 谢谢你的帮助
标签: javascript css switch-statement toggle