plain javascript 隐藏/显示多个带有各自隐藏/显示按钮的区块。

huangapple go评论84阅读模式
英文:

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关系,即parentNodenextElementSibling,而这在我的情况下并不适用,我不知道如何修改它们以适应我的情况。

非常感谢您提供的任何帮助。

英文:

I'd like to have multiple &lt;div&gt; on a page, each with a &lt;button&gt; that toggles the &lt;div&gt;'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 &lt;div&gt; toggled by one &lt;button&gt; 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 &lt;button&gt;s would do the trick, but clicking each of the &lt;button&gt;s results only in the last &lt;div&gt; being toggled as the console.log shows--no matter which &lt;button&gt; is clicked the console shows that &quot;p88c3-button is trying to toggle p88c3-div&quot; .

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 &lt;button&gt; and the &lt;div&gt;, 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(&quot;click&quot;,(e) =&gt; {
  const el = e.target;
  if(el.classList.contains(&quot;button-reply&quot;)){
    const target = document.querySelector(el.dataset.target);
    target.classList.toggle(&quot;active&quot;)
  }
});

<!-- language: lang-css -->

.reply-div {
  display: none;
}

.reply-div.active {
  display: block;
}

<!-- language: lang-html -->

&lt;table border=&quot;1&quot; style=&quot;margin-bottom:5px; margin-left:26px; border:black thin solid;&quot; id=&quot;p88c1&quot;&gt;
  &lt;tr&gt;
    &lt;td align=&quot;right&quot;&gt;&lt;button class=&quot;button-reply&quot; data-target=&quot;#p88c1-div&quot;&gt;reply - p88c1&lt;/button&gt;&lt;/td&gt;
  &lt;/tr&gt;
&lt;/table&gt;
&lt;!-- comment --&gt;
&lt;div class=&quot;reply-div&quot; id=&quot;p88c1-div&quot;&gt;p88c1 - div&lt;/div&gt;
&lt;table border=&quot;1&quot; style=&quot;margin-bottom:5px; margin-left:26px; border:black thin solid;&quot; id=&quot;p88c2&quot;&gt;
  &lt;tr&gt;
    &lt;td align=&quot;right&quot;&gt;&lt;button class=&quot;button-reply&quot; data-target=&quot;#p88c2-div&quot;&gt;reply - p88c2&lt;/button&gt;&lt;/td&gt;
  &lt;/tr&gt;
&lt;/table&gt;
&lt;!-- comment --&gt;
&lt;div class=&quot;reply-div&quot; id=&quot;p88c2-div&quot;&gt;p88c2 - div&lt;/div&gt;
&lt;table border=&quot;1&quot; style=&quot;margin-bottom:5px; margin-left:26px; border:black thin solid;&quot; id=&quot;p88c3&quot;&gt;
  &lt;tr&gt;
    &lt;td align=&quot;right&quot;&gt;&lt;button class=&quot;button-reply&quot; data-target=&quot;#p88c3-div&quot;&gt;reply - p88c3&lt;/button&gt;&lt;/td&gt;
  &lt;/tr&gt;
&lt;/table&gt;
&lt;!-- comment --&gt;
&lt;div class=&quot;reply-div&quot; id=&quot;p88c3-div&quot;&gt;p88c3 - div&lt;/div&gt;

<!-- 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(&quot;click&quot;,(e) =&gt; {
  const el = e.target;
  if(el.classList.contains(&quot;button-reply&quot;)){
    const target = document.querySelector(el.dataset.target);
    target.classList.toggle(&quot;active&quot;)
  }
});

<!-- language: lang-css -->

.reply-div {
  display: none;
}

.reply-div.active {
  display: block;
}

<!-- language: lang-html -->

&lt;table border=&quot;1&quot; style=&quot;margin-bottom:5px; margin-left:26px; border:black thin solid;&quot; id=&quot;p88c1&quot;&gt;
  &lt;tr&gt;
    &lt;td align=&quot;right&quot;&gt;&lt;button class=&quot;button-reply&quot; data-target=&quot;#p88c1-div&quot;&gt;reply - p88c1&lt;/button&gt;&lt;/td&gt;
  &lt;/tr&gt;
&lt;/table&gt;
&lt;!-- comment --&gt;
&lt;div class=&quot;reply-div&quot; id=&quot;p88c1-div&quot;&gt;p88c1 - div&lt;/div&gt;
&lt;table border=&quot;1&quot; style=&quot;margin-bottom:5px; margin-left:26px; border:black thin solid;&quot; id=&quot;p88c2&quot;&gt;
  &lt;tr&gt;
    &lt;td align=&quot;right&quot;&gt;&lt;button class=&quot;button-reply&quot; data-target=&quot;#p88c2-div&quot;&gt;reply - p88c2&lt;/button&gt;&lt;/td&gt;
  &lt;/tr&gt;
&lt;/table&gt;
&lt;!-- comment --&gt;
&lt;div class=&quot;reply-div&quot; id=&quot;p88c2-div&quot;&gt;p88c2 - div&lt;/div&gt;
&lt;table border=&quot;1&quot; style=&quot;margin-bottom:5px; margin-left:26px; border:black thin solid;&quot; id=&quot;p88c3&quot;&gt;
  &lt;tr&gt;
    &lt;td align=&quot;right&quot;&gt;&lt;button class=&quot;button-reply&quot; data-target=&quot;#p88c3-div&quot;&gt;reply - p88c3&lt;/button&gt;&lt;/td&gt;
  &lt;/tr&gt;
&lt;/table&gt;
&lt;!-- comment --&gt;
&lt;div class=&quot;reply-div&quot; id=&quot;p88c3-div&quot;&gt;p88c3 - div&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月30日 04:43:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76584501.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定