2016-11-11 74 views
-4

event.preventDefault();不能在Firefox中工作。event.preventDefault();不工作在Firefox

function submitProffs() { 

     var VacBrand = document.getElementById("txtAcBrand").value; 



     if (VacBrand == "") { 

      alert("Please Select your Ac Brand!"); 
      event.preventDefault(); 
      } 
} 
+1

just in firefox? –

回答

2

function submitProffs(event)

function submitProffs(event) { 
    var vacBrand = document.getElementById("txtAcBrand").value; 
    if (vacBrand == "") { 
     alert("Please Select your Ac Brand!"); 
     event.preventDefault(); 
    } 
} 
1

event应用event是在某些浏览器的全局变量。在Firefox中没有全球的event变量,你不应该依赖这个变量。使用传递给事件处理程序的事件对象。

看来这个变量是其他浏览器(如Google Chrome)已经实现的许多IE非标准功能之一(如document.allinnerText)。

1

你需要传递参数eventsubmitProffs功能

// save global scope 
(function(){ 
'use strict'; 
    // protect from hoisting 
    var mainFunc, 
     submitProffs; 

    submitProffs = function (event) { 
    var VacBrand = document.getElementById("txtAcBrand").value; 
    if (VacBrand == "") { 
     alert("Please Select your Ac Brand!"); 

     event.preventDefault(); 
     // if you need disable bubbling 
     // event.stopPropagation(); 
    } 
    }; 

    mainFunc = function (event) { 
    // listen event on element 
    // replace 'click' to your event 
    document.addEventListener("click", submitProffs, false); 
    }; 
    // code run when html DOM ready 
    document.addEventListener("DOMContentLoaded", mainFunc); 
}()); 

它是如何工作看到有https://jsfiddle.net/2gew6nLo/

+0

是的,我同意。 Javascript有很多功能=) –

0

在FF/Mozilla浏览器的事件被传递给事件处理程序作为一个参数。在IE中使用类似下面的内容来解决丢失的事件参数。

function onlyNumeric(e) 
{ 
    if (!e) { 
     e = window.event; 
    } 

    ... 
}