js事件定義方式和獲取事件物件event總結

2021-09-09 00:12:56 字數 856 閱讀 2088

js中事件的定義方式有3種:標籤內事件屬性,dom元素事件屬性 和 dom元素新增事件的方法。

1.標籤內事件屬性://事件處理函式內this 指向事件源元素

a div

標籤內事件屬性其實是乙個匿名函式,在ie中等價於 $('adiv').οnclick=function()  在ff中 等價於 $('adiv').οnclick=function(event)

因為 在ie中 event物件是在事件發生時建立的,作為window物件的屬性存在,全域性範圍內可訪問;在ff中 event物件是作為事件處理函式的第乙個引數傳入的,函式體內可用 event或arguments[0]引用

2.dom元素事件屬性: //事件處理函式內this 指向事件源元素

$("adiv").οnclick=function(); //ie only. 在ie中 event物件是在事件發生時建立的,作為window物件的屬性存在,全域性範圍內可訪問

$("adiv")['onclick']=function();

$('adiv').οnclick=function(e); //ff, 事件處理函式的第乙個引數就是event物件,引數名稱可自定義 如 e,  ev event....

$('adiv')['onclick']=function(e);

3.dom元素的事件相關方法 //事件處理函式內this不指向 事件源元素,需要用 event.srcelement 或 event.target引用

$('adiv').attachevent('onclick',function());  // ie only

$('adiv').addeventlistener('click',function(e),false); // ff

js 事件 事件物件

關於事件寫了一系列文章 1.事件流 事件捕獲和事件冒泡 2.事件處理程式 3.事件物件 本文介紹事件物件 4.通用的事件偵聽器函式 5.事件迴圈 event loop 6.事件 event delegation 1.事件物件 觸發dom上的某個物件時,會產生乙個事件物件event dom0級事件物件...

js事件物件 事件委託

var div document.getelementsbytagname div 0 div.onclick function e chromeiee window.event firefox iechrome event.target event.srcelement event.target ...

JS 獲取觸發事件的物件

event.srcelement 引發事件的目標物件,常用於onclick事件。event.fromelement 引發事件的物件源,常用於onmouseout和onmouseover事件。event.toelement 引發事件後,滑鼠移動到的目標源,常用於onmouseout和onmouseov...