英文:
plain javascript hide/unhide multiple divisons each with their own hide/unhide button
问题
我想在页面上有多个<div>
,每个都有一个可以切换<div>
可见性的<button>
。
我在https://stackoverflow.com/questions/4528085/toggle-show-hide-div-with-button找到了解决方案,并参考了它,这适用于页面上的一个<div>
由一个<button>
切换。
您可以查看我的尝试:https://jsfiddle.net/pjamesnorris25/8xhtfb1p/17/。
我认为将collection_button[count].onclick = function()
包装在一个for
循环中以覆盖所有<button>
会奏效,但点击每个<button>
只会导致最后一个<div>
被切换,正如控制台中所显示的那样,无论点击哪个<button>
,控制台都会显示"p88c3-button is trying to toggle p88c3-div"
。
因此,明确一下,如何使用纯JavaScript编写代码,以便每次单击"reply - p88c1"
按钮时,它切换"div - p88c1"
的可见性,并且每次单击"reply - p88c2"
按钮时,它切换"div - p88c2"
的可见性,依此类推?
在https://stackoverflow.com/questions/33715084/how-do-i-associate-individual-buttons-with-individual-divs上的两个非jQuery解决方案可以实现我想要的效果,但它们都依赖于<button>
和<div>
之间的DOM关系,即parentNode
和nextElementSibling
,而这在我的情况下并不适用,我不知道如何修改它们以适应我的情况。
非常感谢您提供的任何帮助。
英文:
I'd like to have multiple <div>
on a page, each with a <button>
that toggles the <div>
's visibility.
If found <https://stackoverflow.com/questions/4528085/toggle-show-hide-div-with-button>, and modeled my attempt on it--this is for one <div>
toggled by one <button>
on the page.
You can see my attempt at <https://jsfiddle.net/pjamesnorris25/8xhtfb1p/17/>.
I thought wrapping the collection_button[count].onclick = function()
in a for
loop over all of the <button>
s would do the trick, but clicking each of the <button>
s results only in the last <div>
being toggled as the console.log
shows--no matter which <button>
is clicked the console shows that "p88c3-button is trying to toggle p88c3-div"
.
So to be clear, how can one write the code, using plain javascript, so that each time one clicks on the "reply - p88c1" button
, it toggles the visibility of "div - p88c1" and when one clicks on the "reply - p88c2" button
, it toggles the visibility of "div - p88c2" and so on?
The two non-jQuery solutions at <https://stackoverflow.com/questions/33715084/how-do-i-associate-individual-buttons-with-individual-divs> do what I want, but they both rely on a DOM relationship between the <button>
and the <div>
, i.e., parentNode
and nextElementSibling
, that doesn't obtain in my case, and I don't see how to modify either to work for me.
Thanks in advance for whatever help you can give me.
答案1
得分: 1
以下是您要翻译的内容:
"Instead of unneeded loops, I like to delegate my click handlers to the body. Then inside of it, detect the class of the element clicked to see if matches the class of my buttons.
Instead of ids on the button, I changed that to data-target="#". Where that will be the ID of the div you want toggled.
By default using CSS, I set all of the reply-divs to display none to hide them.
Then in the click handler I simply toggle a class called active that sets the display to block for the target div.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
document.body.addEventListener("click",(e) => {
const el = e.target;
if(el.classList.contains("button-reply")){
const target = document.querySelector(el.dataset.target);
target.classList.toggle("active")
}
});
<!-- language: lang-css -->
.reply-div {
display: none;
}
.reply-div.active {
display: block;
}
<!-- language: lang-html -->
<table border="1" style="margin-bottom:5px; margin-left:26px; border:black thin solid;" id="p88c1">
<tr>
<td align="right"><button class="button-reply" data-target="#p88c1-div">reply - p88c1</button></td>
</tr>
</table>
<!-- comment -->
<div class="reply-div" id="p88c1-div">p88c1 - div</div>
<table border="1" style="margin-bottom:5px; margin-left:26px; border:black thin solid;" id="p88c2">
<tr>
<td align="right"><button class="button-reply" data-target="#p88c2-div">reply - p88c2</button></td>
</tr>
</table>
<!-- comment -->
<div class="reply-div" id="p88c2-div">p88c2 - div</div>
<table border="1" style="margin-bottom:5px; margin-left:26px; border:black thin solid;" id="p88c3">
<tr>
<td align="right"><button class="button-reply" data-target="#p88c3-div">reply - p88c3</button></td>
</tr>
</table>
<!-- comment -->
<div class="reply-div" id="p88c3-div">p88c3 - div</div>
<!-- end snippet -->"
如果您需要更多帮助或其他翻译,请告诉我。
英文:
Instead of unneeded loops, I like to delegate my click handlers to the body. Then inside of it, detect the class of the element clicked to see if matches the class of my buttons.
Instead of ids on the button, I changed that to data-target="#". Where that will be the ID of the div you want toggled.
By default using CSS, I set all of the reply-divs to display none to hide them.
Then in the click handler I simply toggle a class called active that sets the display to block for the target div.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
document.body.addEventListener("click",(e) => {
const el = e.target;
if(el.classList.contains("button-reply")){
const target = document.querySelector(el.dataset.target);
target.classList.toggle("active")
}
});
<!-- language: lang-css -->
.reply-div {
display: none;
}
.reply-div.active {
display: block;
}
<!-- language: lang-html -->
<table border="1" style="margin-bottom:5px; margin-left:26px; border:black thin solid;" id="p88c1">
<tr>
<td align="right"><button class="button-reply" data-target="#p88c1-div">reply - p88c1</button></td>
</tr>
</table>
<!-- comment -->
<div class="reply-div" id="p88c1-div">p88c1 - div</div>
<table border="1" style="margin-bottom:5px; margin-left:26px; border:black thin solid;" id="p88c2">
<tr>
<td align="right"><button class="button-reply" data-target="#p88c2-div">reply - p88c2</button></td>
</tr>
</table>
<!-- comment -->
<div class="reply-div" id="p88c2-div">p88c2 - div</div>
<table border="1" style="margin-bottom:5px; margin-left:26px; border:black thin solid;" id="p88c3">
<tr>
<td align="right"><button class="button-reply" data-target="#p88c3-div">reply - p88c3</button></td>
</tr>
</table>
<!-- comment -->
<div class="reply-div" id="p88c3-div">p88c3 - div</div>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论