EDIT
tôi đã viết một đoạn mã sẽ bắt chước giao diện EventListener và ie8 một, là callable thậm chí trên các đối tượng đơn giản: https://github.com/antcolag/iEventListener/blob/master/iEventListener.js
OLD ĐÁP
này là một cách để addEventListener thi đua hoặc attachEvent trên các trình duyệt không hỗ trợ một trong những yêu cầu
hy vọng này sẽ giúp
(function (w,d) { //
var
nc = "", nu = "", nr = "", t,
a = "addEventListener",
n = a in w,
c = (nc = "Event")+(n?(nc+= "", "Listener") : (nc+="Listener","")),
u = n?(nu = "attach", "add"):(nu = "add","attach"),
r = n?(nr = "detach","remove"):(nr = "remove","detach")
/*
* the evtf function, when invoked, return "attach" or "detach" "Event" functions if we are on a new browser, otherwise add "add" or "remove" "EventListener"
*/
function evtf(whoe){return function(evnt,func,capt){return this[whoe]((n?((t = evnt.split("on"))[1] || t[0]) : ("on"+evnt)),func, (!n && capt? (whoe.indexOf("detach") < 0 ? this.setCapture() : this.removeCapture()) : capt ))}}
w[nu + nc] = Element.prototype[nu + nc] = document[nu + nc] = evtf(u+c) // (add | attach)Event[Listener]
w[nr + nc] = Element.prototype[nr + nc] = document[nr + nc] = evtf(r+c) // (remove | detach)Event[Listener]
})(window, document)
Cho dù trình duyệt triển khai thực hiện kế hoạch thừa kế mẫu cho các đối tượng DOM của nó không liên quan đến việc nó có hỗ trợ W3C [giao diện EventTarget] hay không (http://www.w3.org/TR/DOM-Level-2-Events/events .html # Sự kiện-Sự kiệnTarget-addEventListener). Nếu bạn muốn thử nghiệm hỗ trợ, hãy kiểm tra trực tiếp: 'if (element.addEventListener) {/ * supported * /} else {/ * not supported * /}' có hiệu lực trong tất cả các trình duyệt và độc lập với việc triển khai. – RobG