【问题标题】:What is the Dojo equivalent to jQuery .live()?与 jQuery .live() 等效的 Dojo 是什么?
【发布时间】:2011-07-02 07:07:42
【问题描述】:

与 jQuery .live() 等效的 Dojo 是什么?
http://api.jquery.com/live/

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

【问题讨论】:

    标签: javascript jquery events dojo live


    【解决方案1】:

    用法demo

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

    代码 - 重写了 dojo 原来的​​ mixin-like 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
        }
    });
    

    ticket#11459

    您可以像 jQuery delegate 一样更普遍地使用它,而不仅仅是 live,因为 live 基本上是文档级别的 delegate

    【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      随便用

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

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

      在道场中,很高兴:on(document, ".className", function(){})

      其实jquery.live就是这么做的,就是将事件绑定到文档上来实现功能。

      【讨论】:

        猜你喜欢
        • 2016-05-18
        • 2011-01-31
        • 1970-01-01
        • 1970-01-01
        • 2019-06-26
        • 2011-03-11
        • 2010-11-27
        • 1970-01-01
        • 2015-04-26
        相关资源
        最近更新 更多