2

我注意到这里有几个类似性质的帖子,但没有一个回答我的问题,这就是我发布这个的原因。

我正在尝试制作一个简单的表单,其中每个 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;
        }
    }
}()
} 

非常感谢任何帮助!

4

1 回答 1

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);
于 2012-12-12T16:57:10.347 回答