我已经注意到其他一些类似性质的帖子,但没有一个回答我的问题,这就是我发帖的原因。
我正在尝试创建一个简单的表单,其中每个input / textarea元素在由onblur效果和值===“”触发时变为红色。以下代码适用于Crhome和IE,但在Firefox'事件未定义' - 即使我正在通过事件。
见下面的代码。
//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;
}
}
}()
}
非常感谢任何帮助!
答案 0 :(得分: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);