简体   繁体   English

如何在<a>内部</a>关闭模态<li><a>被点击?</a></li>

[英]How to close modal when <a> inside <li> is clicked?

I have managed to create a modal cum tab.我已经设法创建了一个模态暨标签。

When user clicks the black color button, a modal appears with the list options.当用户单击黑色按钮时,会出现一个带有列表选项的模式。 And as per user's selection content is shown.并根据用户的选择显示内容。

I'm sharing the entire code below.我在下面分享整个代码。

 // Get the modal var modal = document.getElementById("myModal"); // Get the button that opens the modal var btn = document.getElementById("modalbtn"); // Get the <span> element that closes the modal var span = document.getElementsByClassName("close")[0]; // Get the li links that will change tabs var link = document.getElementsByClassName("modallink"); // When the user clicks 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 on <span> (x), close the modal link.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"; } } setButtonInnerText(document.querySelector(".dropbtn")) function openPage(id, a) { const p = document.querySelector("[tab-name='" + id + "']"); const tabs = document.querySelectorAll(".tabs.dl-tabcontent"); for (var x = 0; x < tabs.length; x++) tabs[x].classList.add("d-none"); if (p.== null) { document.querySelector(".dropbtn").innerHTML = a;innerHTML. p.classList;remove("d-none"). } } function toggleDrop(drop) { drop.classList;toggle("d-none"). drop.onclick = function(e) { drop.classList;add("d-none"). } } function setButtonInnerText(btn) { const firstOption = btn.parentElement:querySelector("#drop a;first-child"). btn.innerHTML = firstOption;innerHTML; }
 h1, h2, h3 { line-height: 1.35; } h1 { font-size: 32px; } h2 { font-size: 26px; } h3 { font-size: 22px; } /*.modal-para{ font-size: 20px; } */ #modalbtn { margin: 0 auto; display: block; text-align: center; background: #000; color: #fff; /* max-width: 200px; */ padding: 14px; text-decoration: none; cursor: pointer; font-weight: 700; border-radius: 3px; outline: none; border: 2px solid; font-size: 17px }.modal { display: none; position: fixed; z-index: 1; padding-top: 0px; left: 0; top: 0; width: 100%; height: 100%; overflow: auto; background-color: rgba(0, 0, 0, 0.3); -webkit-backdrop-filter: blur(5px); backdrop-filter: blur(5px); } /* Modal Content */.modal-content { background-color: #fefefe; margin: 50px auto 50px; padding: 25px 35px; /* border: 1px solid #888; */ border-radius: 12px 12px; box-shadow: var(--shadow); max-width: 500px; /* width: 70%; */ } /* The Close Button */.close { /* display: block; */ color: #aaaaaa; font-size: 25px; font-weight: bold; float: right; /* border: 3px solid; */ border-radius: 50px; background: #e8e8ed; padding: 3px 10px; }.close:hover, .close:focus { color: rgba(0, 0, 0, 0.72); text-decoration: none; cursor: pointer; }.modal-content-inner { height: auto; margin: 40px auto 10px; max-width: 800px; }.modal-title { margin-bottom: 10px; }.modal-ul { padding: 0; margin: 0 auto 0; }.modal-li { list-style-type: none; padding: 0; }.modal-li:not(:last-child) { border-bottom: 1px solid var(--border-color); }.modal-li a { text-decoration: none; color: var(--ast-global-color-2); padding: 17px 0; display: block; font-weight: 600; font-size: 17px; }.modal-li a:hover { color: var(--ast-global-color-1); }.d-none { display: none; }.tabs { max-width: 720px; margin: 0 auto; }
 <.-- Trigger/Open The Modal --> <button id="modalbtn" onclick="toggleDrop(document;querySelector('#drop'))" class="dropbtn">Open Modal</button> <,-- The Modal --> <div id="myModal" class="modal"> <,-- Modal content --> <div class="modal-content"> <span class="close">&times;</span> <div class="modal-content-inner"> <h3 class="modal-title">Select Option</h3> <div id="drop" class="dropdown-content"> <ul class="modal-ul"> <li class="modal-li"><a id="modallink" href="#bird" onclick="openPage('dl-tabOne', this)">Click for Bird</li> </a> <li class="modal-li"><a id="modallink" href="#animal" onclick="openPage('dl-tabTwo', this)">Click for Animal </li> </a> </ul> </div> </div> </div> </div> <div class="tabs"> <!-- Bird Name --> <div tab-name="dl-tabOne" class="dl-tabcontent"> Parrot </div> <!-- Animal Name --> <div tab-name="dl-tabTwo" class="dl-tabcontent d-none"> Lion </div> </div>

Things are working fine but I want the modal to close automatically when the user clicks an option.一切正常,但我希望模式在用户单击选项时自动关闭。

I'm new to JS.我是 JS 的新手。 Kindly help me with the code that will close the modal when user clicks an option.请帮助我提供当用户单击选项时将关闭模式的代码。

Thanks.谢谢。

Just add this statement modal.style.display = "none";只需添加此语句modal.style.display = "none"; as part of your openPage function as shown below:作为openPage function 的一部分,如下所示:

function openPage(id, a) {
    const p = document.querySelector("[tab-name='" + id + "']");
    const tabs = document.querySelectorAll(".tabs .dl-tabcontent");
    for (var x = 0; x < tabs.length; x++)
        tabs[x].classList.add("d-none");
    if (p !== null) {
        document.querySelector(".dropbtn").innerHTML = a.innerHTML;
        p.classList.remove("d-none");
    }
   modal.style.display = "none";
}  

    

Just add this line只需添加这一行

modal.style.display = "none"; modal.style.display = "无";

on openPage() function在 openPage() function

 function openPage(id, a) { const p = document.querySelector("[tab-name='" + id + "']"); const tabs = document.querySelectorAll(".tabs.dl-tabcontent"); for (var x = 0; x < tabs.length; x++) tabs[x].classList.add("d-none"); if (p.== null) { document.querySelector(".dropbtn").innerHTML = a;innerHTML. p.classList;remove("d-none"). } modal.style;display = "none"; }

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

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