Stop talking nonsense and just provide the sample code: Copy code The code is as follows: <br> function EventUtil() {<br> var _self = this;<br> ///添加事件<br> var addEvent = (function () {<br> if (document.addEventListener) {<br> return function (el, type, fn) {<br> el.addEventListener(type, fn, false);<br> }<br> } else {<br> return function (el, type, fn) {<br> el.attachEvent("on" type, function () {<br> return fn.call(el, window.event);<br> });<br> }<br> }<br> })();<br> ///添加属性改变事件<br> var addPropertyChangeEvent = function (obj, fn) {<br> if (window.ActiveXObject) {<br> obj.onpropertychange = fn;<br> } else {<br> obj.addEventListener("input", fn, false);<br> }<br> }<br> //移除事件<br> var removeEvent = function (obj, type, fn) {<br> if (obj.removeEventListener) {<br> obj.removeEventListener(type, fn, false);<br> } else if (obj.detachEvent) {<br> obj.detachEvent("on" type, obj["on" type fn]);<br> obj["on" type fn] = null;<br> }<br> }<br> //加载事件<br> var loadEvent = function (fn) {<br> var oldonload = window.onload;<br> if (typeof oldonload != "function") {<br> window.onload = fn;<br> } else {<br> window.onload = function () {<br> oldonload();<br> fn();<br> }<br> }<br> }<br> //阻止事件<br> var stopEvent = function (e) {<br> e = e || window.event;<br> if (e.preventDefault) {<br> e.preventDefault();<br> e.stopPropagation();<br> } else {<br> e.returnValue = false;<br> e.cancelBubble = true;<br> }<br> }<br> //If you just want to prevent events from bubbling<br> var stopPropagation = function (e) {<br> e = e || window.event;<br> if (! "v1") {<br> e.cancelBubble = true;<br> } else {<br> e.stopPropagation();<br> }<br> }<br> //Get the event source object<br> var getEvent1 = function (e) {<br> e = e || window.event;<br> var obj = e.srcElement ? e.srcElement : e.target;<br> return obj;<br> }<br> //Get the event source object<br> var getEvent2 = function (e) {<br> If (window.event) return window.event;<br> var c = getEvent2.caller;<br> while (c.caller) {<br> c = c.caller;<br> }<br> return c.arguments[0];<br> }<br> //Or this function is more powerful<br> var getEvent3 = function (e) {<br> var e = e || window.event;<br> if (!e) {<br> var c = this.getEvent3.caller;<br> while (c) {<br> e = c.arguments[0];<br> If (e && (Event == e.constructor || MouseEvent == e.constructor)) {<br> break;<br> }<br> c = c.caller;<br> }<br> }<br> var target = e.srcElement ? e.srcElement : e.target,<br> currentN = target.nodeName.toLowerCase(),<br> parentN = target.parentNode.nodeName.toLowerCase(),<br> grandN = target.parentNode.parentNode.nodeName.toLowerCase();<br> return [e, target, currentN, ParentN, grandN];<br> } <p> _self.addEvent = addEvent;<br> _self.addPropertyChangeEvent = addPropertyChangeEvent;<br> _self.removeEvent = removeEvent;<br> _self.loadEvent = loadEvent;<br> _self.stopEvent = stopEvent;<br> _self.stopPropagation = stopPropagation;<br> _self.getEvent1 = getEvent1;<br> _self.getEvent2 = getEvent2;<br> _self.getEvent3 = getEvent3;<br> }<br> var eventUtil = new EventUtil();<br> eventUtil.loadEvent(function () {<br> eventUtil.addEvent(document, "click", function (e) {<br> Alert(eventUtil.getEvent3(e));<br> });<br> eventUtil.addPropertyChangeEvent(文檔,函數(e){<br> Alert(eventUtil.getEvent3(e));<br> });<br> });<br> >腳本<br> </p> </div> <p>javascript事件處理分為三個階段:捕獲 - 處理 - 起泡。 </p>以點擊按鈕為例:<p> 捕獲階段:由外層到內層,首先呼叫給Window註冊的點擊捕獲階段監聽方法,document、body、層的父節點,一直到按鈕本身。 <br>處理階段:呼叫按鈕本身的click監聽方法。 </p>起泡階段:從按鈕開始,從內層到外層,依序呼叫父節點的起泡階段監聽方法,直到視窗。 <p>但是,對於IE8及後續版本IE,不支援捕獲階段,捕獲階段的事件監聽目前尚不通用。 </p>通常的事件處理方法形式為:<p> </p> <p></p> <p></p>複製程式碼<p></p> <div class="codetitle"> <span>程式碼如下:<a style="CURSOR: pointer" data="91918" class="copybut" id="copybut91918" onclick="doCopy('code91918')"><u></u> 函數 eventHandler(e) { </a> e = e ||視窗.事件; </span> var 目標 = e.target || e.srcElement; </div> ... ... <div class="codebody" id="code91918"> <br> } <br> <br><br> <br>e為事件對象,當事件觸發時,作為參數確定,但對於IE8及優先版本IE不適用,只能透過全域的事件變數訪問,好在不會出現同時處理兩個事件的情況。 🎜></div>