zoukankan      html  css  js  c++  java
  • JQuery DOM clone(true),对于克隆对象事件触发后,处理函数中this指代克隆对象

    <!doctype html>
    <html ng-app>
        <head>
            <script src="./jquery.js"></script>
        </head>
        <body>
            <div id="template">hello</div>
            <script type="text/javascript">
                $("#template").click(function(){alert("hello id="+$(this).attr("id"))})
    
                var templateClone = $("#template").clone(true);
    
                templateClone.attr("id", templateClone.attr("id")+"_"+1);
                
                templateClone.appendTo("body");
            </script>
        </body>
    </html>

     如上实验, 第二行文字点击, 显示第二个id。

    具体原因是, 事件被克隆, 但是事件处理函数没有被执行, 事件处理函数在clone对象的事件处理后才执行, 其中this为,触发此函数执行的对象。

    见zhangxinxu解释:

    http://www.zhangxinxu.com/wordpress/2012/04/js-dom%e8%87%aa%e5%ae%9a%e4%b9%89%e4%ba%8b%e4%bb%b6/

    ar $ = function(el) {
        return new _$(el);    
    };
    var _$ = function(el) {
        this.el = el;
    };
    _$.prototype = {
        constructor: this,
        addEvent: function() {
            // ...
        },
        fireEvent: function() {
            // ...
        },
        removeEvent: function() {
            // ...
        }
    }
  • 相关阅读:
    CIL中间语言浅谈
    keepalived实现nginx高可用
    centos 开启关闭网卡
    CentOS7下安装httpd服务
    freeswitch socket连接报错
    centos安装nodejs
    CronTrigger
    mysql 表增加索引
    FreeSWITCH呼叫参数之sip_cid_type
    中继网关开启呼入
  • 原文地址:https://www.cnblogs.com/lightsong/p/6230393.html
Copyright © 2011-2022 走看看