以编程方式触发的事件不适用于事件委托
如果有人能帮助我弄清楚为什么在 MooTools 中使用事件委托(来自 Element.Delegation 类)时无法以编程方式触发事件,我将非常感激。
有一个父
,它在某些子
元素上有一个 change
侦听器。当用户操作触发更改事件时,父 div 上的处理程序会被触发,但是当我在任何子输入上使用 fireEvent
以编程方式触发它时,什么也不会发生。基本设置是:html
<div id="listener">
<input type="text" id="color" class="color" />
</div>
js
$("listener").addEvent("change:relay(.color)", function() {
alert("changed!!");
});
$("color").fireEvent("change"); // nothing happens
父 div 上的事件处理程序不会被调用。任何帮助表示赞赏。
相关问题:用 fireEvent
触发的事件在 DOM 树中是否会冒泡?我当前的黑客是本地调度正在运行的事件(但仍然是黑客) - http://jsfiddle。网/SZZ3Z/1/
var event = document.createEvent("HTMLEvents")
event.initEvent("change", true);
document.getElementById("color").dispatchEvent(event); // instead of fireEvent
Would really appreciate if anyone can help me figure out why I am unable to fire events programmatically when using event delegation in MooTools (from the Element.Delegation
class).
There is a parent <div>
that has a change
listener on some child <input>
elements. When the change event is triggered by user actions, the handler on the parent div gets triggered, but when I fire it programmatically with fireEvent
on any child input, nothing happens. The basic setup is:
html
<div id="listener">
<input type="text" id="color" class="color" />
</div>
js
$("listener").addEvent("change:relay(.color)", function() {
alert("changed!!");
});
$("color").fireEvent("change"); // nothing happens
The event handler on the parent div does not get called. Any help is appreciated.
Related question: Do events triggered with fireEvent
bubble at all in the DOM tree? My current hack is to dispatch the event natively which is working (but a hack nonetheless) - http://jsfiddle.net/SZZ3Z/1/
var event = document.createEvent("HTMLEvents")
event.initEvent("change", true);
document.getElementById("color").dispatchEvent(event); // instead of fireEvent
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
“按原样”这样做效果不太好。事件冒泡(以及以编程方式触发事件)的问题在于,它可能需要事件对象是“真实的”,以便它包含与中继字符串匹配的
event.target
。此外,document.id("color").fireEvent()
将不起作用,因为颜色本身没有附加任何事件。为了解决这个问题,您可以通过传递包含目标元素的事件对象来伪造父侦听器上的事件,如下所示:
view in action: http://www.jsfiddle.net/xZFqp/1/
如果您在回调函数中执行诸如 event.stop 之类的操作,那么您需要传递
{target: document. id("color")、stop: Function.from}
等等,对于您可能引用的任何事件方法,但事件委托代码目前只对target
感兴趣。this won't work too well 'as is'. the problem with event bubbling (and with programmatic firing of events) is that it may need the event object to be 'real' in order for it to contain
event.target
that is being matched against the relay string. also,document.id("color").fireEvent()
won't work as color itself has no event attached to it.to get around this, you fake the event on the parent listener by passing an event object that contains the target element like so:
view in action: http://www.jsfiddle.net/xZFqp/1/
if you do things like event.stop in your callback function then you need to pass on
{target: document.id("color"), stop: Function.from}
and so forth for any event methods you may be referencing but the event delegation code is only interested intarget
for now.我是这样解决的:
I resolved it this way: