js事件事件理解

來源:互聯網
上載者:User

標籤:utils   range   document   function   事件   content   事件處理   目標   自己的   

 

js事件事件理解

<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" />     <title>js事件流模型</title></head><style>    #con{        width:200px;        height:200px;        background: orange;    }    #outer{        position: relative;        top: 50px;        left: 50px;        width:100px;        height:100px;        background: #eeddff;    }    #inner{        position: relative;        top: 251px;        left: 25px;        width:50px;        height:50px;        background: #44ddff;    }</style><body><div id="con">con    <div id="outer">    outer        <div id="inner">inner</div>    </div></div><script> </script></body></html>

 

1.執行順序

document->html->body-con->outer->ineer->outer->con->body->html->document

2.響應某個時間的函數叫做事件處理常式

function fnHandler(){
}
<script>    // js事件流    // 事件添加    var con = document.getElementById(‘con‘);    // con.addEventListener(‘click‘,function(){    //     alert(‘123‘);    // });    // con.addEventListener(‘click‘,function(){    //     alert(‘123‘);    // },false);    // con.addEventListener(‘click‘,function(){    //     alert(‘123‘);    // },true);    // DOM事件流// DOM2級事件規定事件流包括三個階段:事件捕獲階段、處於目標階段和事件冒泡階段。// DOM2級事件定義了兩個方法addEventListener()和removeEventListener()// IE不同的它有自己的方法attachEvent()和detachEventvar  eventUtils = {    // addEventHandler:function(){}    addEventHandler: function(el,event,fnHandler){        // console.log(‘test‘);        if(el.addEventListener){            console.log(el.addEventListener);            el.addEventListener(event,fnHandler,true)        }            else{                console.log(el.attachEvent);                el.attachEvent(‘on‘+event,fnHandler)}    },    removeEventHandler: function(el,event,fnHandler){        // console.log(‘test‘);        if(el.removeEventListener){            console.log(el.removeEventListener);            el.removeEventListener(event,fnHandler,true)        }            else{                console.log(el.detachEvent);                el.detachEvent(‘on‘+event,fnHandler)}    }}eventUtils.addEventHandler(con,‘click‘,function(){            alert(‘123‘);    });</script>

 

js事件事件理解

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.