开发者

Is it possible to do live events in YUI, similar to jQuery's live events?

开发者 https://www.devze.com 2023-01-01 19:21 出处:网络
It doesn\'t currently seem as though there is a way to do live events in YUI similar to jQuery: http://api.jquery.com/live/

It doesn't currently seem as though there is a way to do live events in YUI similar to jQuery: http://api.jquery.com/live/

开发者_如何学编程

It sure would be nice if something like:

function handleClick(e) {
    // click!
}

YUI().use('node-base', function(Y) {
    Y.on("click", handleClick, ".foo");
});

caused handleClick to be fired when a node with the "foo" class was clicked on after being dynamically added to the DOM and sometime after the Y.on was evaluated. I feel like live events are one of jQuery's killer features that YUI lacks.


You can use YUI3's support for event delegation to accomplish the same thing.

YUI().use("node", "event", function(Y) {
    Y.delegate("click", function() {
        //click!
    }, Y.config.win, ".foo");
});

I think attaching that to the window is ok, you might need to attach it to the body instead.

Use Y.one("body") instead of Y.config.win in that case.


YUI 2's Event Utility also supports a delegate method.

0

精彩评论

暂无评论...
验证码 换一张
取 消