关于attachEvent与addEventListener事件绑定兼容问题
生活随笔
收集整理的這篇文章主要介紹了
关于attachEvent与addEventListener事件绑定兼容问题
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
attachEvent與addEventListener
attachEvent:是IE支持的事件綁定方法
用法:元素節點對象.attachEvent(“on” + 事件名, 處理函數)
addEventListener:是IE支持的事件綁定方法
用法:元素節點對象.addEventListener(事件名, 處理函數,捕獲或冒泡)
處理兼容函數
在attachEvent與addEventListener兩者間做選擇
function addEvent(el, even, handler_cb){ if(window.attachEvent){ //如果支持attachEvent則輸出該方法return function(){el.attachEvent("on" + even, handler_cb); }//如果只支持addEventListener則輸出該方法,并傳個type進行調用}else if(window.addEventListener){ return function(type){el.addEventListener(even, handler, type)}} }總結
以上是生活随笔為你收集整理的关于attachEvent与addEventListener事件绑定兼容问题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 数据库索引的作用以及索引基本原理
- 下一篇: 使用TraceView+实际案列实战性能