如何点击<li>内的<a>元素时自动关闭Modal弹窗?
点击Modal选项后自动关闭弹窗的实现方案
我已经实现了集成Tab功能的Modal弹窗:点击黑色按钮弹出带选项列表的Modal,选择后展示对应内容。现在需要添加点击选项时Modal自动关闭的功能,以下是具体实现方法:
关键修改点
1. 修正HTML选项链接的标识问题
原HTML中选项链接使用了重复的id="modallink"(id是唯一标识符,不能重复使用),同时存在标签闭合错误,修正如下:
<!-- 修正后的选项列表 --> <ul class="modal-ul"> <li class="modal-li"><a class="modallink" href="#bird" onclick="openPage('dl-tabOne', this)">Click for Bird</a></li> <li class="modal-li"><a class="modallink" href="#animal" onclick="openPage('dl-tabTwo', this)">Click for Animal</a></li> </ul>
2. 在Tab切换函数中添加关闭Modal的逻辑
在openPage函数末尾添加一行代码,完成Tab切换后立即关闭Modal:
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弹窗 modal.style.display = "none"; }
完整代码(修改后)
HTML
<!-- 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">×</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 class="modallink" href="#bird" onclick="openPage('dl-tabOne', this)">Click for Bird</a></li> <li class="modal-li"><a class="modallink" href="#animal" onclick="openPage('dl-tabTwo', this)">Click for Animal</a></li> </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>
CSS
h1, h2, h3 { line-height: 1.35; } h1 { font-size: 32px; } h2 { font-size: 26px; } h3 { font-size: 22px; } #modalbtn { margin: 0 auto; display: block; text-align: center; background: #000; color: #fff; padding: 14px; 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-radius: 12px 12px; box-shadow: var(--shadow); max-width: 500px; } /* The Close Button */ .close { color: #aaaaaa; font-size: 25px; font-weight: bold; float: right; 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; }
JavaScript
// 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 links = 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"; } // 遍历所有选项链接,绑定点击关闭事件(可选,因为openPage里已处理) for(let i = 0; i < links.length; i++){ links[i].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"); } // 新增:关闭Modal弹窗 modal.style.display = "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; }
内容的提问来源于stack exchange,提问作者Subh
相关产品推荐
相关产品推荐

