Javascript進階程式設計第二版第十二章–Event–筆記

來源:互聯網
上載者:User

今天給諸位分享一下 chapter 12 Events

 所謂事件就是頁面與文件視窗發生互動的瞬間。當年事件發生時它可以被預定(程式處理)。

 事件有兩個過程,冒泡過程,或捕獲過程。

 冒泡是自上而下,捕獲是自下而上。
這個順序是document -> html -> body -> onclick element

 

 比如單擊一下一個div,但瀏覽器不認為你只單擊一個dom元素,而是div,body,html,document等這些元素。

 dom一般會處理捕獲或冒泡時的事件,但ie只處理冒泡時,
ie的event 處理 attachEvent(type,fn);
dom addEventListener(type,fn, true|false);

dom addEventListener(type,fn, true|false);
type:事件類型,
fn:事件處理函數
true 在捕獲階段, false 在冒泡階段。

 司徒的通用addEvent:

addEvent:function(el, type, fn ) {
  if(!+"\v1") {
      el['e'+type+fn]=fn;
      el.attachEvent( 'on'+type, function() {
          el['e'+type+fn]();
      } );
  }else{
      el.addEventListener( type, fn, false );
  }
}

這與平常的element.onclick這種有什麼區別呢?

如果給element上添加兩個onclick只會執行一個。但attachEvent添加兩個,會從後往執行。

 對應的刪除事件:detchEvent() removeEventListener()

一般事件分五類:
1. UI事件,
2. 7個滑鼠事件(click,dblclick,mousedown,mouseover,mousemove,mouseup,mouseout,)
3. 3個鍵盤事件( keypress,keydown,keyup)
4. html事件(load,resize,scroll,unload)
5 因dom變化引起的事件

今天大概就這麼多。細節,還要在仔細斟酌。

相關文章

聯繫我們

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