Dojo中相当于jQuery .live()的是什么?

13

Dojo中相当于jQuery .live()的解决方案是什么?
http://api.jquery.com/live/

我找到的唯一解决方案是dojo.disconnect事件处理程序,然后在页面添加动态标记后重新连接它们。

3个回答

14

使用方法演示

dojo.query("body").delegate(selector, eventName, fn);

code - 重写了Dojo中原始的类似于mixin的delegate函数

dojo.provide("dojox.NodeList.delegate");
dojo.require("dojo.NodeList-traverse");
dojo.extend(dojo.NodeList, {
    delegate: function ( selector,eventName, fn) {
        return this.connect(eventName, function (evt) {
            var closest = dojo.query(evt.target).closest(selector, this);
            if (closest.length) {
                fn.call(closest[0], evt);
            }
        }); //dojo.NodeList
    }
});

参见票号#11459

你可以像使用 jQuery 的 delegate 一样普遍使用这个方法,不仅仅是作为 live,因为 live 实际上就是针对文档级别的 delegate


2
仅为澄清起见,delegate函数应该在完整版Dojo 1.6中可用,通过要求dojox.NodeList.delegate(此模块可以在答案链接的工单结尾处的更改集中看到)。如果您感兴趣,目前可以使用1.6 RC。http://download.dojotoolkit.org/release-1.6.0rc1/ - Ken Franqueiro
是的,自1.6版本以来就可用:http://dojotoolkit.org/reference-guide/dojox/NodeList/delegate.html#dojox-nodelist-delegate - Michaël Witrant

1

我认为dojo.behavior执行类似的功能


0

只需使用

on(document, "xxx", function(){})

例如在jQuery中:$(".className").live(function(){})

在Dojo中,它相当于:on(document, ".className", function(){})

实际上这就是jQuery.live所做的,它将事件绑定到文档上以实现该功能。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接