英文:
Toggle the visibility of an element when a button is clicked
问题
我正在尝试执行一个练习,通过访问元素的ID来切换元素的可见性。当我点击它时,我可以使其消失。然而,我不明白我的代码有什么问题,以至于它在点击后不会重新出现。
let button = document.getElementById("button");
document.getElementById("button").onclick = function() {
if (button.style.display === "none") {
button.style.display = "block";
} else {
button.style.display = "none";
}
};
#button {
display: block;
}
<p id="p1Text">Hello world</p>
<button id="button">Buttonni</button>
希望这可以帮助你解决问题。
英文:
I am trying to perform an exercise where I toggle the visibility of an element by accessing its ID. I can get it to disappear when I click it. However, I don't understand what is wrong with my code so that it doesn't reappear when clicked.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
let button = document.getElementById("button");
document.getElementById("button").onclick = function() {
if (button.style.display === "none") {
button.style.display = "block";
} else {
button.style.display = "none";
}
};
<!-- language: lang-css -->
#button {
display: block;
}
<!-- language: lang-html -->
<p id="p1Text">Hello world</p>
<button id="button">Buttonni</button>
<!-- end snippet -->
答案1
得分: 1
我建议使用element.classList.toggle
来实现这种情况。
https://www.w3schools.com/howto/howto_js_toggle_class.asp
const hello = document.querySelector('.hello');
document.getElementById("button").onclick = function() {
hello.classList.toggle('hidden');
};
.hidden {
display: none;
}
<p id="p1Text" class="hello">Hello world</p>
<button id="button">Buttonni</button>
英文:
I would refer to use element.classList.toggle to achieve this senario.
https://www.w3schools.com/howto/howto_js_toggle_class.asp
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const hello = document.querySelector('.hello');
document.getElementById("button").onclick = function() {
hello.classList.toggle('hidden');
};
<!-- language: lang-css -->
.hidden {
display: none;
}
<!-- language: lang-html -->
<p id="p1Text" class="hello">Hello world</p>
<button id="button">Buttonni</button>
<!-- end snippet -->
答案2
得分: 0
你点击按钮后让按钮消失了。这是你想要让它消失的意思吗?
还是你想在点击按钮后让以下段落消失?
<p id="p1Text">Hello world</p>
如果你想让这段文字消失,你需要将 getElementById
更改为正确的ID。
目前你这样做:
let button = document.getElementById("button");
这获取到了按钮的ID,相反,你应该获取段落的元素:
let button = document.getElementById("p1Text");
这应该可以解决问题。
英文:
You are making the button disappear once it is clicked. Is that what you mean to make disappear?
Or are you trying to make the following paragraph disappear upon button click?
<p id="p1Text">Hello world</p>
If that is what you want to make disappear, you have to change the getElementById
to the correct ID.
Currently, you are doing:
let button = document.getElementById("button");
which is getting the ID of the button, instead, get the element of the paragraph
let button = document.getElementById("p1Text");
That should fix it
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论