在对象上调用特定事件的默认操作 [英] Invoke the default action of a specific event on an object

查看:82
本文介绍了在对象上调用特定事件的默认操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一种通用(跨浏览器)方式让浏览器为指定事件的对象执行默认操作,最好不要获取任何附加的侦听器解雇

I want a generic (cross browser) way to get the browser to execute the default action for an object for a specified event preferably without getting any attached listeners to fire.

之前已经提出过这类问题,但是在已经参加活动,&/或严格地谈论同步推迟在以后发生之前的默认操作

This kind of question has been asked before but in the context of already being in an event, &/or strictly talking about synchronously holding off the default action before letting it happen later.

基本上,假设你有一个DOM元素(你不知道哪个)变量,你想要调用点击,这样如果它是锚点,它就会跟随 href ,如果是输入[type =submit] ,它会提交表格。
但是没有为点击事件触发该元素的侦听器(可能会出现 preventDefault(听众中的 s。

Basically, let's say you have an DOM element (you don't know which) variable, and you want to invoke a click, such that if it were an anchor, it'd follow its href, if it were an input[type="submit"] it would submit the form.
But without firing that element's listeners for the click event (which may be prone preventDefault()s in the listeners).

对于那些需要理由回答问题的人;如果您正在实施 <$ c $之类的内容,可能想要这样做c>只有 <$ c的浏览器的dispatchEvent() $ c> fireEvent() 。

两个函数返回 true 如果默认值不是'阻止',但只有 dispatchEvent()实际上是通过并调用默认值。

For those that need a reason to answer questions; you may want to do it if you're implementing something like dispatchEvent() for browsers that only have fireEvent().
Both functions return true if the default wasn't 'prevented', but only dispatchEvent() actually follows through and invokes the default.

作为一个附带问题,jQuery的 trigger() 是意味着这样做(虽然在触发听众之后)并且是跨浏览器,也许解决方案在那里?和jQuery一样好,我想知道它调用的vanilla方法(如果它[这个特殊功能]确实适用于像IE8这样的东西)。

As a side question, jQuery's trigger() is meant to do this (although after firing the listeners) and be cross-browser, maybe the solution is there? As nice as jQuery is I'd like to know the vanilla methods it is calling (if it [this particular feature] indeed works on things like IE8).

推荐答案

看来,至少对于点击事件,你可以调用 .click()在这些浏览器中的元素。

这将使元素执行默认值(以及调用所有侦听器和 .onclick 值,如果已定义)。

我不知道DOM元素的任何其他默认值,滚动是否需要计算? 按键时?也许那些作为函数存在,但我还没有测试过。

It appears that, at least for the click event, you can call .click() on the element in these browsers.
This will have the element enact the default (as well as calling all listeners and the .onclick value, if defined).
I don't know of any other defaults on DOM elements, would scroll need to be accounted for? Keypress? Maybe those exist as functions too, but I haven't tested.

当我测试 .click() ,我注意到它听从了任何一个返回false的监听器并且没有触发默认值,并且事件确实起了泡沫(我在文档上的监听器也被解雇了!)。

When I did test .click(), I noticed it obeyed any of the listeners returning false and did not fire the default, and the event did bubble (a listener I had on document fired too!).

可能没有其他选择(即直接调用默认值,避免使用侦听器)。至于传递一个事件对象,有问题的浏览器会发生单个全局事件,所以也许你可以操作它,并且在执行 .click()

There may be no other alternative (I.e. calling the default directly, avoiding the listeners). And as for passing it an event object, the browser in question has the single global event thing going on, so maybe you can manipulate that and it wont have changed when executing .click().

添加,因为我在旅行中找到更多信息:)

此外,有一个 doScroll() 函数,但我认为将给定事件转换为此方法接受的字符串真的很难

Adding as I find more information in my travels :)
Furthermore, there is an doScroll() function, but I think it would be really hard to translate a given event into the string accepted by this method

这篇关于在对象上调用特定事件的默认操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆