简体   繁体   English

使用锚标记打开模态时,页面重定向到空白链接

[英]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"> &times; </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.

相关问题 使用php单击打开模态的锚标记时获取模态上​​的ID - Get the id on modal when click on anchor tag for open modal using php 使用锚标记链接打开标签页? - Open tab using anchor tag link? 使用Window.Open时,原始页面重定向,而新窗口为空白 - Original page redirects while new window is blank when using Window.Open 使用锚点/链接元素打开特定页面,并自动在该页面上打开模式 - use an anchor/link element to open a particular page, and automatically open a modal on that page 使用定位标记打开空白窗口并写入空白窗口 - Use anchor tag to open a blank window and write into it HTML-链接到打开的页面,然后在该页面上定位 - HTML - link to open page, then to anchor on that page 仅在定位标记链接时使用网页上的 Tab 按钮导航时,我想让空格键充当回车键单击 - While navigating using Tab button on a web page only when at a anchor tag link, I want to make spacebar act as a enter key click 如何使用打开jQuery模式对话框 <input type=“button”> 而不是锚标签? - How to open jQuery modal dialog using <input type=“button”> instead of anchor tag? 自动点击jsp页面上的锚标签链接 - automatically click anchor tag link on jsp page 锚标签链接到具有动画的不同页面上的ID - Anchor tag link to ID on different page with animate
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM