深入淺析JavaScript中對(duì)事件的三種監(jiān)聽(tīng)方式
事件(Event)是JavaScript應(yīng)用跳動(dòng)的心臟,也是把所有東西粘在一起的膠水,當(dāng)我們與瀏覽器中Web頁(yè)面進(jìn)行某些類型的交互時(shí),事件就發(fā)生了。
第一種監(jiān)聽(tīng)方式,也是最普遍使用的方式,是直接在代碼上加載事件,產(chǎn)生效果:
<table> <tr onmouseover='this.style.backgroundColor="red"' onmouseout='this.style.backgroundColor=""'><td>text1</td><td>text2</td></tr> <tr onmouseover='this.style.backgroundColor="red"' onmouseout='this.style.backgroundColor=""'><td>text3</td><td>text4</td></tr> <tr onmouseover='this.style.backgroundColor="red"' onmouseout='this.style.backgroundColor=""'><td>text5</td><td>text5</td></tr> </table>
第二種監(jiān)聽(tīng)方式,是使用DOM的方式獲取對(duì)象,并加載事件:
<table> <tr><td>text1</td><td>text2</td></tr> <tr><td>text3</td><td>text4</td></tr> <tr><td>text5</td><td>text5</td></tr> </table> <script> doms = document.getElementsByTagName('tr'); for(i=0;i<doms.length;i++) { doms[i].onmouseover = function() { this.style.backgroundColor = "red"; } doms[i].onmouseout = function() { this.style.backgroundColor = ""; } } </script>
第三種監(jiān)聽(tīng)方式,是使用標(biāo)準(zhǔn)的addEventListener方式和IE私有的attachEvent方式,因?yàn)镮E的attachEvent方式在參數(shù)傳遞時(shí)的缺陷,這個(gè)問(wèn)題被搞得稍許有些復(fù)雜了:
<table> <tr><td>text1</td><td>text2</td></tr> <tr><td>text3</td><td>text4</td></tr> <tr><td>text5</td><td>text5</td></tr> </table> <script> doms = document.getElementsByTagName('tr'); function show_color(where) { this.tagName ? where = this : null where.style.backgroundColor = "red"; } function hide_color(where) { this.tagName ? where = this : null where.style.backgroundColor = ""; } function for_ie(where,how) { return function() { how(where); } } for(i=0;i<doms.length;i++) { try { doms[i].addEventListener('mouseover',show_color,false); doms[i].addEventListener('mouseout',hide_color,false); } catch(e) { doms[i].attachEvent('onmouseover',for_ie(doms[i],show_color)); doms[i].attachEvent('onmouseout',for_ie(doms[i],hide_color)); } } </script>
在綁定多個(gè)相同的事件的時(shí)候,前兩種方法會(huì)產(chǎn)生覆蓋,而第三中方法則會(huì)同時(shí)執(zhí)行多個(gè)事件。
- JavaScript 事件監(jiān)聽(tīng)實(shí)例代碼[兼容IE,firefox] 含注釋
- JavaScript 監(jiān)聽(tīng)textarea中按鍵事件
- javascript在事件監(jiān)聽(tīng)方面的兼容性小結(jié)
- javascript 傳統(tǒng)事件模型構(gòu)造的事件監(jiān)聽(tīng)器實(shí)現(xiàn)代碼
- 在JavaScript中監(jiān)聽(tīng)I(yíng)ME鍵盤輸入事件
- javascript監(jiān)聽(tīng)鼠標(biāo)滾輪事件淺析
- JavaScript監(jiān)聽(tīng)和禁用瀏覽器回車事件實(shí)例
- JavaScript監(jiān)聽(tīng)文本框回車事件并過(guò)濾文本框空格的方法
相關(guān)文章
JS簡(jiǎn)單測(cè)試循環(huán)運(yùn)行時(shí)間的方法
這篇文章主要介紹了JS簡(jiǎn)單測(cè)試循環(huán)運(yùn)行時(shí)間的方法,涉及針對(duì)javascript中for循環(huán)、for...in循環(huán)及foreach循環(huán)的相關(guān)使用方法及運(yùn)行時(shí)間測(cè)試,需要的朋友可以參考下2016-09-09原生JS封裝_new函數(shù)實(shí)現(xiàn)new關(guān)鍵字的功能
這篇文章主要介紹了原生JS封裝_new函數(shù),實(shí)現(xiàn)new關(guān)鍵字的功能 ,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-08-08echarts報(bào)錯(cuò):Error?in?mounted?hook的解決方法
最近又遇到了寫echarts的時(shí)候常遇到的一個(gè)錯(cuò)誤,這篇文章主要給大家介紹了關(guān)于echarts報(bào)錯(cuò):Error?in?mounted?hook:?“TypeError:?Cannot?read?properties?of?undefined?(reading?‘init‘)“的解決方法,需要的朋友可以參考下2022-07-07微信小程序?qū)崿F(xiàn)tab點(diǎn)擊切換
這篇文章主要為大家詳細(xì)介紹了微信小程序?qū)崿F(xiàn)tab點(diǎn)擊切換,不滑動(dòng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-07-07原生JavaScript實(shí)現(xiàn)購(gòu)物車效果
這篇文章主要為大家詳細(xì)介紹了原生JavaScript實(shí)現(xiàn)購(gòu)物車效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-07-07Javascript遍歷Html Table示例(包括內(nèi)容和屬性值)
這篇文章主要介紹了Javascript如何遍歷Html Table(包括內(nèi)容和屬性值),需要的朋友可以參考下2014-07-07