简体   繁体   English

How to display div with same ID and OnClick function - Javascript, HTML and CSS

[英]How to display div with same ID and OnClick function - Javascript, HTML and CSS

I'm fairly new to Javascript, and i've reached an issue I can't figure out yet, so I'll explain it as best as I can.我对 Javascript 还很陌生,我遇到了一个我还无法弄清楚的问题,所以我会尽可能地解释它。

I've got 2 divs containing a reply link with the same ID, OnClick.我有 2 个 div,其中包含具有相同 ID、OnClick 的回复链接。 Only difference is the data-attribute which I thought could be used to differentiate the two.唯一的区别是我认为可以用来区分两者的数据属性。 There are 2 reply divs that are styled to be hidden.有 2 个回复 div 的样式设置为隐藏。 The aim is once the reply link is clicked, the correct div will display below it.目的是一旦点击回复链接,正确的 div 将显示在其下方。

The issue is, when you click any of the two Reply links, it only opens the first reply div below the first parent div.问题是,当您单击两个回复链接中的任何一个时,它只会打开第一个父 div 下方的第一个回复 div。 I'll created a little example to give a better understanding:我将创建一个小示例以更好地理解:

 // Opens reply div and retrieves data-attribute (reply_id) to insert into MYSQL database function replyLink(element) { document.getElementById('reply').style.display = "block"; } // Close div link, displays after opening reply box function closeLink() { document.getElementById('reply').style.display = "none"; }
 #comment{ border: 1px solid #333333; width: 500px; height: 85px; padding: 5px; margin: 10px 10px 15px 10px; } #comment #content{ border: none; padding: 15px; font-size: 12px; } #comment #link{ border: none; padding: 5px; margin-top: 5px; } #comment #link a{ border: none; text-decoration: none; font-size: 12px; color: blue; } #comment #link a:hover{ border: none; text-decoration: underline; font-size: 12px; color: blue; } #reply{ border: 1px solid red; padding: 15px; margin: 0px 0px 10px 45px; width: 400px; }
 <div id="comment"> <div id="content"> Content #1 </div> <div id="link"> <a href='javascript:void(0);' onclick="replyLink()" data-test='1'>Reply</a> </div> </div> <div id="reply" style="display: none;"> reply container 1 <a href='javascript:void(0);' onclick='closeLink()' />[Close]</a> </div> <div id="comment"> <div id="content"> Content #2 </div> <div id="link"> <a href='javascript:void(0);' onclick="replyLink()" data-test='2'>Reply</a> </div> </div> <div id="reply" style="display: none;"> reply container 2 <a href='javascript:void(0);' onclick='closeLink()' />[Close]</a> </div>

Would a java genius be able to help me out. java 天才能否帮助我。

You can use the classes for styling and IDs with indexes to identify the unique div boxes.您可以使用样式类和带有索引的 ID 来识别唯一的 div 框。 Here is the working example这是工作示例

 function replyLink(index) { document.getElementById('reply_' + index).style.display = "block"; } // Close div link, displays after opening reply box function closeLink(index) { document.getElementById('reply_' + index).style.display = "none"; }
 .comment { border: 1px solid #333333; width: 500px; height: 85px; padding: 5px; margin: 10px 10px 15px 10px; }.comment.content { border: none; padding: 15px; font-size: 12px; }.comment.link { border: none; padding: 5px; margin-top: 5px; }.comment.link a { border: none; text-decoration: none; font-size: 12px; color: blue; }.comment.link a:hover { border: none; text-decoration: underline; font-size: 12px; color: blue; }.reply { border: 1px solid red; padding: 15px; margin: 0px 0px 10px 45px; width: 400px; }
 <div class="comment"> <div class="content"> Content #1 </div> <div class="link"> <a href='javascript:void(0);' onclick="replyLink(0)" data-test='1'>Reply</a> </div> </div> <div class="reply" id="reply_0" style="display: none;"> reply container 1 <a href='javascript:void(0);' onclick='closeLink(0)'>[Close]</a> </div> <div class="comment"> <div class="content"> Content #2 </div> <div class="link"> <a href='javascript:void(0);' onclick="replyLink(1)" data-test='2'>Reply</a> </div> </div> <div class="reply" id="reply_1" style="display: none;"> reply container 2 <a href='javascript:void(0);' onclick='closeLink(1)'>[Close]</a> </div>

While the use of an id is straightforward when first working with JavaScript and HTML, it's use is discouraged as an anti-pattern.虽然在第一次使用 JavaScript 和 HTML 时使用 id 很简单,但不鼓励将其用作反模式。 IDs make for brittle code (as you are seeing here) and don't scale well. ID 会产生脆弱的代码(正如您在此处看到的那样)并且不能很好地扩展。 Instead, don't use ids at all and instead use classes or a relative reference to the elements, such as this, .closest(), nextElementSibling, parentNode, etc.相反,根本不要使用 id,而是使用类或对元素的相对引用,例如 this、.closest()、nextElementSibling、parentNode 等。

Also, using hyperlinks as a "hook" to initiate some code upon a click event is semantically incorrect.此外,使用超链接作为“钩子”在单击事件时启动某些代码在语义上是不正确的。 Hyperlinks are for navigation and people who use screen readers will have difficulty navigating your page.超链接用于导航,使用屏幕阅读器的人将难以导航您的页面。 Just about every visible HTML element supports a click event, so just attach a click handler directly to the element instead of wrapping the element with a hyperlink.几乎每个可见的 HTML 元素都支持单击事件,因此只需将单击处理程序直接附加到元素,而不是用超链接包装元素。

Lastly, there is no need for separate show and hide functions.最后,不需要单独的显示和隐藏功能。 Just add or remove a "hidden" class based on what was clicked.只需根据单击的内容添加或删除“隐藏的” class 即可。

You can see in my answer how much cleaner the HTML and JavaScript are without id s.您可以在我的回答中看到 HTML 和 JavaScript 没有id的清洁程度。

See comments inline below.请参阅下面的内联评论。

 // Set up a single event handler for any clicks to any reply or Close document.addEventListener("click", function(event){ // Check to see if the click originated at a Reply element if(event.target.classList.contains("reply")){ // Find the closest ".comment" ancestor of the clicked reply // element and then get the next element sibling to that and // unhide it. event.target.closest(".comment").nextElementSibling.classList.remove("hidden"); } else if(event.target.classList.contains("replyContainer")){ event.target.classList.add("hidden"); } });
 .hidden { display:none; }.comment{ border: 1px solid #333333; width: 500px; height: 85px; padding: 5px; margin: 10px 10px 15px 10px; }.comment.reply{ padding: 5px; margin-top: 5px; }.replyContainer{ border: 1px solid red; padding: 15px; margin: 0px 0px 10px 45px; width: 400px; }
 <div class="comment"> <div class="content">Content #1</div> <div class="reply">Reply</div> </div> <div class="hidden replyContainer">reply container 1[Close]</div> <div class="comment"> <div class="content">Content #2</div> <div class="reply">Reply</div> </div> <div class="hidden replyContainer">reply container 2[Close]</div> <div class="comment"> <div class="content">Content #3</div> <div class="reply">Reply</div> </div> <div class="hidden replyContainer">reply container 3[Close]</div>

You can use the DOM structure to your advantage.您可以使用 DOM 结构来发挥自己的优势。 Normally you wouldn't want to rely too deeply on it though.通常,您不会希望过深地依赖它。 For toggling div it's perfectly ok.对于切换div完全没问题。 By using element which was clicked we can look for closest common ancestor.通过使用被点击的元素,我们可以寻找最近的共同祖先。 That way we can find the related div .这样我们就可以找到相关的div

 function replyLink(elem) { elem.closest("article").querySelector("#reply").style.display = "block"; } function closeLink(elem) { elem.closest("article").querySelector("#reply").style.display = "none"; } // just for demo: document.body.innerHTML = document.body.innerHTML + document.body.innerHTML document.body.innerHTML = document.body.innerHTML + document.body.innerHTML
 <article> <div id="comment"> <div id="content"> Content #N </div> <div id="link"> <a href='javascript:void(0);' onclick="replyLink(this)" data-test='1'>Reply</a> </div> </div> <div id="reply" style="display: none;"> reply container 1 <a href='javascript:void(0);' onclick='closeLink(this)'>[Close]</a> </div> </article>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM