[英]dispatchEvent causes page reload
I have been working with some modal code demonstrated at CodePen . 我一直在使用CodePen演示的一些模态代码。 Nice Material design style modals but as demonstrated, to activate, they essentially require a click on an <a>
tag which has a class of modal__trigger
. 很好的Material设计风格模态,但如要演示的那样,要激活它们,本质上需要单击具有modal__trigger
类的<a>
标签。 In the demo page, they are restyled as buttons. 在演示页面中,它们被重新设置为按钮样式。
I want to programmatically trigger a modal and so I have created the following code. 我想以编程方式触发模式,因此我创建了以下代码。 The first line is the trigger. 第一行是触发器。 The remainder is the modal. 其余为模态。
<a href="" id="coordmodal-trigger" data-modal="#coord-modal" class="modal__trigger"></a> <div id="coord-modal" class="modal modal__bg" role="dialog" aria-hidden="true"> <div class="modal__dialog"> <div class="modal__content"> <h4>Location</h4> <p id="coordinates">-28.8050, 149.9483</p> <!-- modal close button --> <a id="coordmodal-close" href="" class="modal__close"> <svg class="" viewBox="0 0 24 24"><path d="M19 6.41l-1.41-1.41-5.59 5.59-5.59-5.59-1.41 1.41 5.59 5.59-5.59 5.59 1.41 1.41 5.59-5.59 5.59 5.59 1.41-1.41-5.59-5.59z"/><path d="M0 0h24v24h-24z" fill="none"/></svg> </a> </div> </div> </div>
Loading this in a page, I can see the trigger <a>
. 将其加载到页面中,可以看到触发器<a>
。 When I click on it, the modal appears with it's content - all good. 当我单击它时,模态随它的内容一起出现-一切都很好。
However, from the console, simulating a click, when I type the following: 但是,在控制台中,当我键入以下命令时,将模拟单击:
var foo = document.getElementById('coordmodal-trigger'); foo.dispatchEvent(new Event('click'));
the modal appears without content and then the page reloads. 出现的模态没有内容,然后重新加载页面。 I'm puzzled as to why this is occurring given that I am simply programatically producing a click event. 鉴于我只是以编程方式产生click事件,所以我对为什么会发生这种情况感到困惑。 Which should be the same as clicking on the link, should it not? 哪个应该与单击链接相同,不是吗?
Which should be the same as clicking on the link, should it not 应该与点击链接相同,否则
No, events programmatically created have the property cancelable
defaulting to false
. 否,以编程方式创建的事件具有cancelable
属性, 默认为false
。 This means calling preventDefault()
does not stop the default action. 这意味着调用preventDefault()
不会停止默认操作。 And since you are clicking on an anchor you need to prevent the default navigation action. 并且由于您单击的是锚点,因此您需要阻止默认的导航操作。
You need to pass in an EventInit dictionary with the cancelable
property set to true
您需要传入一个EventInit字典,并将cancelable
属性设置为true
new Event('click',{cancelable:true});
This will allow preventDefault()
to stop the default action 这将允许preventDefault()
停止默认操作
Demo 演示版
var link = document.querySelector("a"); var btn = document.querySelector("#second"); var btn2 = document.querySelector("#third"); link.addEventListener("click",function(e){ e.preventDefault(); if(e.cancelable) document.body.insertAdjacentHTML('beforeend','<br>Event canceled<br>'); }); btn.addEventListener("click",function(){ var evt = new Event("click",{cancelable:true}); link.dispatchEvent(evt); }); btn2.addEventListener("click",function(){ var evt = new Event("click"); link.dispatchEvent(evt); });
<a href="http://www.stackoverflow.com">Click First</a><br> <button id="second">Click Second (simulates click with cancelable set)</button><br> <button id="third">Click Third (simulates click without cancelable set)</button>
As for the content not showing that is more likely due to the fact that since the page is navigating away the js is being unloaded and stops at some point before it has had a chance to load it. 至于该内容未显示的可能性更大,原因是由于该页面正在导航,因此正在卸载js,并且在有机会加载js之前在某个点停止。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.