gpt4 book ai didi

javascript - Y.Node addTarget 事件不冒泡

转载 作者:行者123 更新时间:2023-12-02 18:28:17 26 4
gpt4 key购买 nike

为什么我的事件没有在 foo 中触发并冒泡到 bar

var foo = Y.one(".foo"),
bar = Y.one(".bar");

foo.addTarget(bar);

foo.on("myEvent", function () {
//this log statement executes
Y.log("In foo listener");
});

bar.on("myEvent", function () {
//this log statement doesn't execute.
//I don't understand why. I think it
//should because I expect myEvent to
//bubble from foo to bar since I used
//addTarget
Y.log("In bar listener");
});

foo.fire("myEvent");

JS fiddle :http://jsfiddle.net/steaks/tJnLf/

最佳答案

您必须从 foo 发布 myEvent 并将 emitFacade 设置为 truehttp://yuilibrary.com/yui/docs/event-custom/#facade

http://jsfiddle.net/tJnLf/27/

YUI().use('event-custom', 'node', function(Y) {
Y.on('domready',function(e) {
var foo = Y.one(".foo"),
bar = Y.one(".bar");

foo.publish("myEvent", {
emitFacade: true
});

foo.addTarget(bar);

foo.on("myEvent", function () {
Y.log("In foo listener");
});

bar.on("myEvent", function () {
Y.log("In bar listener");
});

foo.fire("myEvent");
});
});

关于javascript - Y.Node addTarget 事件不冒泡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18118042/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com