简体   繁体   English

Javascript:停止onClick默认

[英]Javascript: Stop onClick default

I get the feeling I am missing something obvious but just can see it. 我感到自己缺少明显的东西,但可以看到。

A trigger on the click event is set: 设置了click事件的触发器:

 anchor.addEventListener("click", myClickEvent ); function myClickEvent() { this.preventDefault(); // fails return false; } 

However the default onClick method still happens. 但是,默认的onClick方法仍然会发生。

Can I cancel default propogation at the same time as I addEventListener or is there some other way of doing this. 我可以在添加事件监听器的同时取消默认传播,还是可以使用其他方法来取消传播。 I cannot use .preventDefault() from within myClickEvent because the event is not available at that point. 我无法在myClickEvent中使用.preventDefault(),因为该事件当时不可用。

Edit following comments for clarity: The default onClick of the tag happens even though myClickEvent returns false. 为了清楚起见,请编辑以下注释:即使myClickEvent返回false,也会发生标记的默认onClick。

Returning false from a handler that was registered with addEventListener does nothing. 从使用addEventListener注册的处理程序返回false不会执行任何操作。 See https://stackoverflow.com/a/1357151/227299 and https://developer.mozilla.org/en-US/docs/Web/API/Event/preventDefault 参见https://stackoverflow.com/a/1357151/227299https://developer.mozilla.org/en-US/docs/Web/API/Event/preventDefault

The recommended way is to call event.preventDefault() 推荐的方法是调用event.preventDefault()

function myClickEvent(e) {
  e.preventDefault()
}

Handlers from HTML attributes still work, but you should not use it since it's not as clear what return false means when compared to event.preventDefault() (not allow the link to be followed) and event.stopPropagation() (don't let the event bubble up) 来自HTML属性的处理程序仍然有效,但是您不应该使用它,因为与event.preventDefault() (不允许遵循链接)和event.stopPropagation() (不要让它return false event.preventDefault()相比, return false含义还不清楚事件冒泡了)

element.addEventListener("click", myClickEvent );

function myClickEvent(event) {
  event.preventDefault();
  return false;
}

There is no such function in the global scope called preventDefault -- you need to use the event that has been passed into your callback. 全局范围中没有名为preventDefault此类函数-您需要使用已传递到回调中的事件。

Also please make sure that you are attaching the event to something that can be clicked on (a form, anchor, button etc) that can potentially have an event prevented. 还请确保您在事件附加的东西,可以点击(一种形式,锚,按钮等), 可以潜在地具有一个事件阻止。

If you attach the event to a child/parent element nothing will work as you expected. 如果将事件附加到子元素/父元素,则将无法按预期工作。

Nearly there thanks to Teemu's code but not quite. 几乎在这里感谢Teemu的代码,但不完全是。

There is a difference depending on how the function is called. 根据调用函数的方式而有所不同。 Seemingly there is both a hidden parameter sent defining the value of 'this' and when called without parameters an automatic event object sent as the first visible parameter: 似乎既发送了定义'this'值的隐藏参数,又调用了不带参数的自动事件对象作为第一个可见参数:

Call it without parameters: 不带参数调用它:

 anchor.addEventListener("click", myClickEvent ); function myClickEvent(param1, param2) { // this = events parent element (<a>) // this.href = <a href=...> // param1 = event object // param2 = undefined oEvent = param1; oEvent.preventDefault(); } 

but then call it with parameters: 但是然后使用参数调用它:

 anchor.addEventListener("click", function (oEvent) { myClickEvent(oEvent, myParameter); }); function myClickEvent(param1, param2) { // this = [object Window] // this.href = undefined // param1 = event object // param2 = myParameter oEvent = param1; oEvent.preventDefault(); } 

Notice how when parameters are sent manually the value of 'this' changes from the events parent element to '[object Window]', causing this.href to go wrong. 请注意,当手动发送参数时,“ this”的值从事件父元素变为“ [object Window]”,从而导致this.href出错。

However the preventDefault now appears to be working reliably. 但是,preventDefault现在似乎可以正常工作。

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

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