[英]Page redirects to a blank link when using anchor tag to open a modal
I am trying to open a modal window using the anchor tag. 我正在尝试使用锚标签打开模态窗口。 The modal window successfully opens.
模态窗口成功打开。 However, the page redirects to a random blank page upon opening the modal window.
但是,打开模态窗口时,页面会重定向到随机空白页面。 How do I fix this?
我该如何解决?
//script for modal var modal = document.getElementById("modalAdd"); var btn = document.getElementById("addMissing"); var span = document.getElementsByClassName("close")[0]; btn.onclick = function() { modal.style.display = "block"; } span.onclick = function() { modal.style.display = "none"; } window.onclick = function(event) { if (event.target == modal) { modal.style.display = "none"; } }
<!-- anchor --> <a data-toggle="modal" href="#modalAdd" id="addMissing"> Add </a> <!-- modal div --> <div id="modalAdd"> <div id="modalAdd-content"> <span class="close"> × </span> <p> Modal Content </p> </div> </div>
You should probably use button
to open a modal, but here's solution for your issue: 您应该使用
button
打开模态,但这里是您的问题的解决方案:
btn.onclick = function(event) {
event.preventDefault(); // this will prevent default action
modal.style.display = "block";
}
用href =“javascript:void(0)”替换你的href标签,并通过数据模型获取值
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.