以编程方式触发的事件不适用于事件委托

发布于 2024-08-30 04:33:31 字数 1089 浏览 13 评论 0原文

如果有人能帮助我弄清楚为什么在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

2024-09-06 04:33:31

“按原样”这样做效果不太好。事件冒泡(以及以编程方式触发事件)的问题在于,它可能需要事件对象是“真实的”,以便它包含与中继字符串匹配的 event.target 。此外,document.id("color").fireEvent() 将不起作用,因为颜色本身没有附加任何事件。

为了解决这个问题,您可以通过传递包含目标元素的事件对象来伪造父侦听器上的事件,如下所示:

document.id("listener").fireEvent("change", {
    target: document.id("color")
});

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:

document.id("listener").fireEvent("change", {
    target: document.id("color")
});

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 in target for now.

记忆で 2024-09-06 04:33:31

我是这样解决的:

document.addEvents({
    'click:relay(.remove_curso)': function(){
        document.fireEvent('_remove_curso', this);
    },
    '_remove_curso':function( me ){
        console.log( me );
    }.bind( this )
}

I resolved it this way:

document.addEvents({
    'click:relay(.remove_curso)': function(){
        document.fireEvent('_remove_curso', this);
    },
    '_remove_curso':function( me ){
        console.log( me );
    }.bind( this )
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文