繁体   English   中英

如何在我的网站上使用多个模态?

[英]How can I use multiple modals on my site?

试图弄清楚如何在我的网站上使用多种模式。 我有一个工作正常,但尝试使用第二个触发相同的 javascript 操作,但它没有弹出模态。 我需要一个系统来制作多个模态,因为我的想法是让每个模态都有一个显示 dicord 用户名的弹出框。 我在下面粘贴了我的代码:

 // Get the modal var modal = document.getElementById('myModal'); // Get the button that opens the modal var btn = document.getElementById("myBtn1"); // Get the <span> element that closes the modal var span = document.getElementsByClassName("close")[0]; // When the user clicks on the button, open the modal btn.onclick = function() { modal.style.display = "block"; } // When the user clicks on <span> (x), close the modal span.onclick = function() { modal.style.display = "none"; } // When the user clicks anywhere outside of the modal, close it window.onclick = function(event) { if (event.target == modal) { modal.style.display = "none"; } }
 .modal { display: none; /* Hidden by default */ position: fixed; /* Stay in place */ z-index: 1; /* Sit on top */ left: 0; top: 0; width: 100%; /* Full width */ height: 100%; /* Full height */ overflow: auto; /* Enable scroll if needed */ background-color: rgb(0, 0, 0); /* Fallback color */ background-color: rgba(0, 0, 0, 0.4); /* Black w/ opacity */ } /* Modal Content/Box */ .modal-content { background-color: #7289da; margin: 15% auto; /* 15% from the top and centered */ padding: 20px; border: 1px solid #888; width: 10%; /* Could be more or less, depending on screen size */ } /* The Close Button */ .close { color: #aaa; float: right; font-size: 28px; font-weight: bold; } .close:hover, .close:focus { color: black; text-decoration: none; cursor: pointer; }
 <li id="myBtn1"><a><i class="fa fa-"><center><img height="20px" src="/images/icons/discord.png"></center></i></a></li> <div id="myModal" class="modal"> <!-- Modal content --> <div class="modal-content"> <span class="close">&times;</span> <h3 style="color:white;font-weight:bold;">Discord</h3> <p style="color:white;font-weight:bold;font-style: italic;">text</p> </div> </div>

细节)

您可以通过使用querySelectorAll轻松实现这一点,这将返回 DOM 元素的 HTMLCollection。 然后您可以使用如下所述的forEach函数,我个人认为这种方法比使用传统的for循环更具可读性。

聚苯乙烯

HTMLCollectionArray ,如果您想将HTMLCollection转换为Array ,您可以通过Array.from(HTMLCollection) 还有其他方法可以将[].slice.call(HTMLCollection) Array的对象转换为Array ,即[].slice.call(HTMLCollection) ,如果您希望支持旧版浏览器或仅不支持Array.from浏览器,您可能希望使用后一种方法.

 // Self invoked function. (function() { // List all of the buttons. var btns = document.querySelectorAll("ul li a"); // List all of the modals. var modals = document.querySelectorAll(".modal"); // The on click handler. var onClickAction = function(index) { // Get the modal by index. var modal = modals[index]; if (modal != null) { modal.style.display = "block"; // Now get the span. var closeBtn = modal.querySelector(".close"); closeBtn.onclick = function() { modal.style.display = "none"; }; // Re-assign the window event. window.onclick = function(event) { if (event.target == modal) { modal.style.display = "none"; } } }; }; // Loop over each button. btns.forEach(function(btn, index) { btn.onclick = function() { onClickAction(index); }; }); })();
 .modal { display: none; /* Hidden by default */ position: fixed; /* Stay in place */ z-index: 1; /* Sit on top */ left: 0; top: 0; width: 100%; /* Full width */ height: 100%; /* Full height */ overflow: auto; /* Enable scroll if needed */ background-color: rgb(0, 0, 0); /* Fallback color */ background-color: rgba(0, 0, 0, 0.4); /* Black w/ opacity */ } /* Modal Content/Box */ .modal-content { background-color: #7289da; margin: 15% auto; /* 15% from the top and centered */ padding: 20px; border: 1px solid #888; width: 10%; /* Could be more or less, depending on screen size */ } /* The Close Button */ .close { color: #aaa; float: right; font-size: 28px; font-weight: bold; } .close:hover, .close:focus { color: black; text-decoration: none; cursor: pointer; }
 <ul> <li id="myBtn1"> <a> <i class="fa fa-"> <center><img height="20px" src="/images/icons/discord.png"></center> </i> </a> </li> <li id="myBtn2"> <a> <i class="fa fa-"> <center><img height="20px" src="/images/icons/discord.png"></center> </i> </a> </li> </ul> <div id="myModal" class="modal"> <div class="modal-content"> <span class="close">&times;</span> <h3 style="color:white;font-weight:bold;">Discord</h3> <p style="color:white;font-weight:bold;font-style: italic;">text - 1</p> </div> </div> <div id="myModal2" class="modal"> <div class="modal-content"> <span class="close">&times;</span> <h3 style="color:white;font-weight:bold;">Discord</h3> <p style="color:white;font-weight:bold;font-style: italic;">text - 2</p> </div> </div>

暂无
暂无

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

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