我一直在尝试编写一个代码来模拟这个例子(下面的代码)。
function closeMe(){
x=document.getElementById("demo");
x.style.display="none";
}
function openMe(){
x=document.getElementById("demo");
x.style.display="block";
}
<h1>Changing the Style</h1>
<p>JavaScript can change the style of an HTML element.</p>
<button type="button" onclick="openMe()">Open!</button>
<button type="button" onclick="closeMe()">Close!</button>
<p id="demo">Extra details...You can open and close this paragraph using the buttons above.</p>
不幸的是,在我下面的代码中,当我点击“打开”按钮时,所有的东西都消失了。我不明白为什么。
function open() {
x = document.getElementById("demo");
x.style.display = "block";
}
function close() {
x = document.getElementById("demo");
x.style.display = "none";
}
<p id="demo">Click me to change my HTML content (innerHTML).</p>
<p> this shouldnt disappear</p>
<button onclick="open()">open button</button>
<button onclick="close()">close button</button>
谁能给我一些好的建议?
最佳答案
将显示设置为 block 时,在“ block ”两边添加引号
function open(){
x=document.getElementById("demo");
x.style.display="block";
}
关于javascript - 单击按钮,一切都消失了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53815776/