gpt4 book ai didi

javascript - 事件未在 Firefox 中定义

转载 作者:行者123 更新时间:2023-11-29 17:19:44 25 4
gpt4 key购买 nike

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

我正在尝试制作一个简单的表单,其中每个 input/textarea 元素在被 onblur 效果和值 === ""触发时变为红色。以下代码适用于 Chrome 和 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)});

所以,您可以在底部看到,我正在使用 blurCheckfocusCheck 函数处理我的事件,并传入事件。所以我不知道为什么 Fiefox 不承认它!!

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;
}
}
}()
}

非常感谢任何帮助!

最佳答案

您忘记传递事件对象。它只是在旧的 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);

关于javascript - 事件未在 Firefox 中定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13844706/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com