[JS學習筆記]淺談Javascript事件模型

來源:互聯網
上載者:User

標籤:

DOM0級事件模型element.on[type] = function(){}相容性:全部支援 lay1lay2lay3
  • e.target:直接觸發事件的元素[IE8及以下不支援taget屬性,使用e.srcElement代替]
  • e.srcElement:直接觸發事件的元素[FF不支援srcElement屬性,使用e.target代替]
  • e.currentTarget:被間接觸發的元素[IE8及以下不支援currentTarget屬性,使用this代替]
  • this:同e.currentTarget[所有瀏覽器通用]
DOM2級事件模型attachEvent相容性:IE5-IE10支援,IE11不支援,ff不支援 lay1lay2lay3
  • 對於支援的瀏覽器
  • e.target:不支援該屬性
  • e.srcElement:直接觸發事件的元素
  • e.currentTarget:不支援該屬性
  • this:始終指向window,可以使用call/apply改變this指向
    element.attachEvent("on"+type,function(){ handler.apply(element,arguments); })
DOM2級事件模型addEventListener相容性:IE5-IE8不支援,IE9-IE11支援,ff支援 lay1lay2lay3
  • 對於支援的瀏覽器
  • e.target:直接觸發事件的元素
  • e.srcElement:不支援該屬性
  • e.currentTarget:被間接觸發的元素
  • this:同e.currentTarget
$(function() {            //dom0級事件綁定            var eleArr_d1 = $.makeArray($(".demo1").find(".layer1,.layer2,.layer3"));            $.each(eleArr_d1, function (i, o) {                o.onclick = fn0;            });            function fn0(e) {                e = e || window.event;                console.log("e.target=", getName(e.target));                console.log("e.srcElement=", getName(e.srcElement));                console.log("e.currentTarget=", getName(e.currentTarget));                console.log("this=", getName(this));                console.log("========================================================");            }            //dom2級事件綁定[IE]            var eleArr_d2 = $.makeArray($(".demo2").find(".layer1,.layer2,.layer3"));            if (document.attachEvent) {                $.each(eleArr_d2, function(i, o) {                    o.attachEvent("onclick", function() { fn2_ie.apply(o,arguments); });//改變this指向                });            } else {                $(".demo2").find(".error").text("當前瀏覽器不支援attachEvent").show().css({ display: "inline-block" });            }            function fn2_ie(e) {                e = e || window.event;                console.log("e.target=", getName(e.target));                console.log("e.srcElement=", getName(e.srcElement));                console.log("e.currentTarget=", getName(e.currentTarget));                console.log("this=", getName(this));                console.log("========================================================");            }            //dom2級事件綁定[FF]            var eleArr_d3 = $.makeArray($(".demo3").find(".layer1,.layer2,.layer3"));            if (document.addEventListener) {                $.each(eleArr_d3, function(i, o) {                    if (o.addEventListener) o.addEventListener("click", fn3_ff, false);                });            } else {                $(".demo3").find(".error").text("當前瀏覽器不支援addEventListener").css({display:"inline-block"});            }            function fn3_ff(e) {                e = e || window.event;                console.log("e.target=", getName(e.target));                console.log("e.srcElement=", getName(e.srcElement));                console.log("e.currentTarget=", getName(e.currentTarget));                console.log("this=", getName(this));                console.log("========================================================");            }            function getName(ele) {                if (ele==null) {                    return ele;                }else if (ele.className) {                    return ele.className;                } else if (ele.tagName) {                    return ele.tagName;                }else if (ele.nodeName) {                    return ele.nodeName;                } else {                    return ele;                }            }        })

[JS學習筆記]淺談Javascript事件模型

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.