Dojo中相当于jQuery .live()的解决方案是什么?
http://api.jquery.com/live/
我找到的唯一解决方案是dojo.disconnect事件处理程序,然后在页面添加动态标记后重新连接它们。
Dojo中相当于jQuery .live()的解决方案是什么?
http://api.jquery.com/live/
我找到的唯一解决方案是dojo.disconnect事件处理程序,然后在页面添加动态标记后重新连接它们。
使用方法和演示
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
。
我认为dojo.behavior执行类似的功能
只需使用
on(document, "xxx", function(){})
例如在jQuery中:$(".className").live(function(){})
在Dojo中,它相当于:on(document, ".className", function(){})
实际上这就是jQuery.live所做的,它将事件绑定到文档上以实现该功能。
delegate
函数应该在完整版Dojo 1.6中可用,通过要求dojox.NodeList.delegate
(此模块可以在答案链接的工单结尾处的更改集中看到)。如果您感兴趣,目前可以使用1.6 RC。http://download.dojotoolkit.org/release-1.6.0rc1/ - Ken Franqueiro