我的网站上有这个显示/隐藏按钮。它有效,但第一次用户需要双击它,就好像开关设置为“隐藏”但元素已经隐藏......
我想编辑我的代码,以便按钮在第一次单击时显示该元素
我是 javascript 新手,所以我不知道如何改变它。
谢谢
function showhidemenu() {
var x = document.getElementById("menu");
if (x.style.display === "none") {
x.style.display = "block";
} else {
x.style.display = "none";
}
}
#menu {
background: rgba(0, 0, 0, 0);
position: absolute;
z-index: 1;
top: 60px;
right: 50px;
width: 150px;
font-family: 'Open Sans', sans-serif;
display: none;
}
<div id="menu">This is a menu</div>
<button onclick="showhidemenu()">Show/hide</button>