2012-12-12 68 views
2

我注意到其他一些類似性質的帖子,但沒有回答我的問題,這就是爲什麼我發佈這個。未在Firefox中定義的事件

我試圖做一個簡單的表單,每個input/textarea元素在被onblur效果和值===「」觸發時變成紅色。下面的代碼可以和Crhome和IE一起工作,但是在Firefox'event is not defined'中 - 即使我正在通過一個事件。

請參閱下面的代碼。

//declare gloabl variables 
var srcForm = document.myForm; 
var nameField = srcForm.nameTxt; 
var emailField = srcForm.emailTxt; 
var commentsField = srcForm.comments; 

//function for field onfocus events 
function focusClear (event) { 
var eSrc = nuahs.eventUtility.eventSource(event); 
if(eSrc.className === "invalid") { 
    eSrc.className = ""; 
    eSrc.value = "";  
} 
} 

//function for field onblur events 
function blurCheck (event) { 
var eSrc = nuahs.eventUtility.eventSource(event); 
if(eSrc.value === "") { 
    eSrc.className = "invalid"; 
    eSrc.value = "This field is required"; 
} 
} 

//set up event handlers for focus events on each field 
nuahs.eventUtility.addEvent(nameField, "focus", function() { focusClear(event)}); 
nuahs.eventUtility.addEvent(emailField, "focus", function() { focusClear(event)}); 
nuahs.eventUtility.addEvent(commentsField, "focus", function() { focusClear(event)}); 

//set up event handlers for focus events on each field 
nuahs.eventUtility.addEvent(nameField, "blur", function() { blurCheck(event)}); 
nuahs.eventUtility.addEvent(emailField, "blur", function() { blurCheck(event)}); 
nuahs.eventUtility.addEvent(commentsField, "blur", function() { blurCheck(event)}); 

所以,你可以看到 - 在底部 - 我處理我與blurCheck和focusCheck功能的事件,並通過事件。所以我不知道FF爲什麼不認識它!

的nuahs對象僅僅是另一種語言的簡單事件實用,但對於完整性你可以在下面看到它:

var nuahs = new Object(); 

nuahs.eventUtility = { 
addEvent: function() { 
    if(typeof addEventListener !== "undefined") { 
     return function(obj, evt, fn) { 
      obj.addEventListener(evt, fn, false); 
     }; 
    } else { 
     return function(obj, evt, fn) { 
      obj.attachEvent("on" + evt, fn);  
     }; 
    } 
}(), 
removeEvent: function() { 
    if(typeof addEventListener !== "undefined") { 
     return function(obj, evt, fn) { 
      obj.removeEventListener(evt, fn, false);  
     }; 
    } else { 
     return function(obj, evt, fn) { 
      obj.detachEvent("on" + evt, fn);  
     }; 
    } 
}(), 
eventSource: function(event) { 
    if (typeof addEventListener !== "undefined") { 
     return event.target;  

    } else { 
     return event.srcElement;  
    } 
}, 
preventDefault: function() { 
    if (typeof addEventListener !== "undefined") { 
     return function(event) { 
      event.preventDefault(); 
     } 
    } else { 
     return function(event) { 
      event.returnValue = false; 
     } 
    } 
}(), 
stopEvent: function() { //to stop event capture and bubbling 
    if (typeof addEventListener !== "undefined") { 
     return function(event) { 
      event.stopPropagation();  
     } 
    } else { 
     return function(event) { 
      event.cancelBubble = true; 
     } 
    } 
}() 
} 

任何幫助,非常感謝!

回答

5

你忘了傳遞事件對象。它只是舊版IE中的全球。

nuahs.eventUtility.addEvent(nameField, "focus", function(event) { focusClear(event)}); 
nuahs.eventUtility.addEvent(emailField, "focus", function(event) { focusClear(event)}); 
nuahs.eventUtility.addEvent(commentsField, "focus", function(event) { focusClear(event)}); 

甚至更​​好,保存this的情況下,您的實用程序庫將其設置爲一個適當的值:

nuahs.eventUtility.addEvent(nameField, "focus", focusClear); 
nuahs.eventUtility.addEvent(emailField, "focus", focusClear); 
nuahs.eventUtility.addEvent(commentsField, "focus", focusClear); 
+0

顯然,它在Chrome的全球太(按問題)。 –

+2

顯然鉻決定迎合誰寫蹩腳的(即:對於IE)代碼:http://stackoverflow.com/questions/8838635/event-preventdefault-working-in-chrome-not-firefox-for-a-submit-按鈕:「Chrome會將骨骼投射到爲IE編寫的代碼。」 – ThiefMaster

+0

噢,我回答了這個問題! :-D –

相關問題