阻止默認事件event.preventDefault();

阻止瀏覽器默認事件。什麼是默認事件,例如瀏覽器默認右鍵菜單、a標籤默認鏈接跳轉...,如何阻止呢?html

 Firefox中,event必須做爲參數傳入。瀏覽器

 IE中,event是window對象的屬性。spa

event.preventDefault();方法用於取消默認事件,可是不兼容IE,在IE下,要用event.returnValue=false;來處理。code

document.oncontextmenu=function (ev) {
    var oEvent=ev||event;
    if (oEvent.preventDefault) {
        oEvent.preventDefault();
    } else{
        oEvent.returnValue=false;
    }
}

 還有,return false (點進去進入詳解)也能阻止默認事件。htm

相關文章
相關標籤/搜索