jQuery:如何将event.preventDefault()与自定义事件一起使用?

时间:2021-05-08 19:53:51

How can I know in my triggering code that preventDefault has been called?

我怎么知道在我的触发代码中已经调用了preventDefault?

$(document).trigger('customEvent', params);
if (/* ??? */)
    doDefaultActions();

4 个解决方案

#1


45  

trigger() can also take an event object, so if you can create an event object, like so:

trigger()也可以接受一个事件对象,所以如果你可以创建一个事件对象,如下所示:

var event = jQuery.Event("customEvent");
$(document).trigger(event);

then you can check after the trigger to see if preventDefault() has been called like so:

然后你可以在触发器后检查,看看是否已经调用了preventDefault():

var prevented = event.isDefaultPrevented();

#2


1  

If you're asking how to find out whether or not the default has been prevented, use:

如果您询问如何确定是否已阻止默认值,请使用:

event.isDefaultPrevented()

event.isDefaultPrevented()

This will return 'true' or 'false' based on whether or not preventDefault() was called.

这将根据是否调用preventDefault()返回'true'或'false'。

EDIT: http://api.jquery.com/event.isDefaultPrevented/

编辑:http://api.jquery.com/event.isDefaultPrevented/

#3


1  

Custom events do not have some default actions that happens .. (they are custom).

自定义事件没有发生的某些默认操作..(它们是自定义的)。

On the other hand, if you want to stop the bubbling effect of this event to others then have a look at triggerHandler which does not bubbles up to the hierarchy ..

另一方面,如果你想要阻止这个事件的冒泡效果给别人,那么看一下不会冒泡到层次结构的triggerHandler。

#4


0  

To my knowledge the "preventDefault()" call is about preventing the native browser responses to things like clicks on anchor tags or keypresses in text fields. Once the event handling cycle is over, it's over. For made-up events, I don't think it has any effect at all since it's all about the jQuery event processing system and not about native browser functionality.

据我所知,“preventDefault()”调用是关于阻止本机浏览器对文本字段中锚点标记或按键点击等内容的响应。一旦事件处理周期结束,它就结束了。对于虚构的事件,我认为它根本没有任何影响,因为它是关于jQuery事件处理系统而不是本机浏览器功能。

Your code could set some sort of flag somewhere in order to communicate with the "outside world."

你的代码可以在某处设置某种标志,以便与“外部世界”进行通信。

[edit] ooh you could try having the handler stash a reference to the event object somewhere that the exteral code can find it, and then externally check with "isDefaultPrevented()". I don't know whether that'd work however.

[编辑]哦,您可以尝试让处理程序存储对事件对象的引用,该代码可以在外部代码可以找到它,然后在外部检查“isDefaultPrevented()”。我不知道那是否有用。

#1


45  

trigger() can also take an event object, so if you can create an event object, like so:

trigger()也可以接受一个事件对象,所以如果你可以创建一个事件对象,如下所示:

var event = jQuery.Event("customEvent");
$(document).trigger(event);

then you can check after the trigger to see if preventDefault() has been called like so:

然后你可以在触发器后检查,看看是否已经调用了preventDefault():

var prevented = event.isDefaultPrevented();

#2


1  

If you're asking how to find out whether or not the default has been prevented, use:

如果您询问如何确定是否已阻止默认值,请使用:

event.isDefaultPrevented()

event.isDefaultPrevented()

This will return 'true' or 'false' based on whether or not preventDefault() was called.

这将根据是否调用preventDefault()返回'true'或'false'。

EDIT: http://api.jquery.com/event.isDefaultPrevented/

编辑:http://api.jquery.com/event.isDefaultPrevented/

#3


1  

Custom events do not have some default actions that happens .. (they are custom).

自定义事件没有发生的某些默认操作..(它们是自定义的)。

On the other hand, if you want to stop the bubbling effect of this event to others then have a look at triggerHandler which does not bubbles up to the hierarchy ..

另一方面,如果你想要阻止这个事件的冒泡效果给别人,那么看一下不会冒泡到层次结构的triggerHandler。

#4


0  

To my knowledge the "preventDefault()" call is about preventing the native browser responses to things like clicks on anchor tags or keypresses in text fields. Once the event handling cycle is over, it's over. For made-up events, I don't think it has any effect at all since it's all about the jQuery event processing system and not about native browser functionality.

据我所知,“preventDefault()”调用是关于阻止本机浏览器对文本字段中锚点标记或按键点击等内容的响应。一旦事件处理周期结束,它就结束了。对于虚构的事件,我认为它根本没有任何影响,因为它是关于jQuery事件处理系统而不是本机浏览器功能。

Your code could set some sort of flag somewhere in order to communicate with the "outside world."

你的代码可以在某处设置某种标志,以便与“外部世界”进行通信。

[edit] ooh you could try having the handler stash a reference to the event object somewhere that the exteral code can find it, and then externally check with "isDefaultPrevented()". I don't know whether that'd work however.

[编辑]哦,您可以尝试让处理程序存储对事件对象的引用,该代码可以在外部代码可以找到它,然后在外部检查“isDefaultPrevented()”。我不知道那是否有用。