问题描述
似乎jQuery的 trigger()
只运行与jQuery绑定的事件处理程序。我有一些使用本机浏览器事件绑定的模块。使用中的代码对我有用,但我想知道是否有内置的东西进入jQuery会做到这一点?
It seems that jQuery's trigger()
only runs event handlers that were bound with jQuery. I have some modules that use native browser event binding. Using the code from https://stackoverflow.com/a/2676527 works for me, but I'm wondering if there's something built in to jQuery that will do this?
从评论更新:显然它适用于按钮上的点击事件。但不适用于选择框上的更改事件:
Update from comments: apparently it works for click events on buttons. But not for change events on select boxes: http://jsfiddle.net/qxpXV/2/
对于记录:黑客攻击其他库与jQuery进行绑定确实 make trigger()
工作,但我真的不想这样做。
For the record: hacking the other library to do its bindings with jQuery does make trigger()
work, but I don't really want to do that.
推荐答案
你可以通过手动触发/来做到这一点直接在DOM元素上调度事件(取决于浏览器, fireEvent
/ dispatchEvent
)。代码将处理事件调度,您只需要针对DOM元素而不是jQuery执行它包装。
You can do this by manually firing/dispatching an event (depending on the browser, fireEvent
/dispatchEvent
) directly on the DOM element. Code from this answer will handle the event dispatching, you'll just need to execute it against a DOM element and not the jQuery wrapper.
这篇关于使用jQuery触发真实事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!