javascript使用eventutil綁定事件

來源:互聯網
上載者:User

我們都知道在使用dom綁定事件的時候,分為以下三種方式:

<!--直接在標籤中指定事件--><button onclick="javascript:alert('first onclick')">first button</button><!--首先得到便簽元素,然後為標籤指定特定事件--><button id="second">second button</button>var second = document.getElementById("second"); second.onclick = function() {    alert("second click");}<!--首先得到便簽元素,然後為通過addEventListener或者attachEvent來指定特定事件--><button id="third">third button</button>var third = document.getElementById("third");    third.addEventListener("click",function(e){        alert("third click");    },false);<button id="forth">forth button</button>var forth = document.getElementById("forth");    forth.attachEvent("onclick",function(e){        alert("forth click");    },false);

對於第三種事件綁定方式是目前大部分網站所採用的,不同的瀏覽器添加事件的方式可能會不同,目前分為上面這兩種:

標籤.attachEvent("onclick",處理方法,該事件是否繼續傳遞)標籤.addEventListener("click",處理方法,該事件是否繼續傳遞)

那麼如何才能使我們添加的事件在不同的瀏覽器上都可以執行呢。。此時我們可以封裝一個eventutil對象,來屏蔽瀏覽器的區別。 eventutil對象的使用

<input type="button" id="myBtn1" value="look"/><script type="text/javascript">    window.onload = function() {        var EventUtil = {        addHandler: function(element, type, handler){    //若瀏覽器支援addEventListener,則使用addEventListener來添加事件            if(element.addEventListener){                element.addEventListener(type, handler, false);            } else if(element.attachEvent){                element.attachEvent("on" + type, handler);            } else {            //若以上兩種添加事件的方法都不支援,則使用預設的行為來添加事件                element["on" + type] = handler;            }        },        //移除事件        removeHandler: function(element, type, handler){            if (element.removeEventListener){                element.removeEventListener(type, handler, false);            } else if(element.detachEvent){                element.detachEvent("on" + type, handler);            } else{                element["on" + type] = null;            }        }   }var btn1 = document.getElementById("myBtn1");var handler = function(){    alert("hello handler");}EventUtil.addHandler(btn1, "click", handler);}

簡單說明一下,我定義了一個eventutil對象,在該對象中有addHandler和removeHandler兩個方法,分別用來添加和移除事件的。此時只需要這樣調用就可以了。、

EventUtil.addHandler(需要綁定事件的標籤, "需要綁定的事件", "綁定事件的處理方法");EventUtil.removeHandler(需要刪除事件的標籤, "需要刪除的事件", "綁定事件的處理方法");

此時,使用eventutil綁定的事件在各個瀏覽器上都是可以啟動並執行了。 使用preventDefault()屏蔽瀏覽器預設行為

同時我們還可以使用preventDefault()方法來屏蔽瀏覽器預設的行為。並且添加自己想要的行為。

<a href="http://www.baidu.com" id="stop">stop default</a><script type="text/javascript">window.onload = function() {    var stop = document.getElementById("stop");    stop.onclick = function(e) {         e.preventDefault();         alert("it has stop the default behavior");    }   }</script>

可以看到,這裡我給a標籤設定了click方法,並且在該方法的第一行,就調用了e.preventDefault();來阻止預設的開啟網頁連結的行為,後邊寫上自己想要的行為即可。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.