Javascript 聚焦时触发模糊事件

Javascript 聚焦时触发模糊事件,javascript,events,javascript-events,event-handling,Javascript,Events,Javascript Events,Event Handling,我正在使用以下代码: 问题是每次聚焦该场时都会触发“模糊”事件,这与您预期的相反。尽管代码中甚至没有提到焦点事件,但这是事实。问题是我无法在JSFIDLE中复制这个问题,但问题发生在IE中 另外,在JSFIDLE上,还有另一个问题。焦点事件被触发多次 对此是否有可能的解释和/或解决方案 更新: 奖金问题(最后一个问题,承诺)。 我添加了一个函数addEvent来动态地将事件添加到表单字段中,而不是直接将它们添加到父构造函数中。这是最好的选择。我试图调用该函数,但它似乎不起作用。我可能做错了什么?

我正在使用以下代码:

问题是每次聚焦该场时都会触发“模糊”事件,这与您预期的相反。尽管代码中甚至没有提到焦点事件,但这是事实。问题是我无法在JSFIDLE中复制这个问题,但问题发生在IE中

另外,在JSFIDLE上,还有另一个问题。焦点事件被触发多次

对此是否有可能的解释和/或解决方案

更新: 奖金问题(最后一个问题,承诺)。
我添加了一个函数addEvent来动态地将事件添加到表单字段中,而不是直接将它们添加到父构造函数中。这是最好的选择。我试图调用该函数,但它似乎不起作用。我可能做错了什么?

您的
焦点
处理程序中的
警报
一旦获得焦点,就会立即将焦点从字段中移除。失焦会触发模糊。奇怪的是,
blur
首先出现

如果将警报更改为
console.log
(或不窃取焦点的内容),您将看到事件正确触发


您知道,当您四处点击时,一个字段的
焦点
是另一个字段的
模糊
对吗?什么是
对象
函数?那
FormTitle
函数是什么?您可能只包含相关部分,而不是所有重复的
click
/
blur
/
change
/
dbclick
方法(只是表明它们有更多)嗯,您不想问关于原型继承而不是关于
blur
问题吗?因此,您应该发布一个完整的工作示例来演示这个问题(可能也是一个)@user-而不是
var n=Object.create(t.prototype)
var n=t.prototype似乎工作正常。是的,没错。现在可以了。顺便问一下,你能看看最新的问题吗?具体地说,是。我尝试使用addEvents方法动态地向原型对象添加事件,而不是在父构造函数中分配所有事件。但它似乎不起作用。我可能做错了什么?当没有名为
focus
的变量时,您将
focus
作为参数传递。我相信您希望传递字符串
“focus”
。哇,我真的需要一些咖啡,就像现在一样!非常感谢,詹姆斯!作为专家,您对事件处理的这种方法(原型)有何评论?
function Field(args) {
    this.id = args.id;

    this.name = args.name ? args.name : null;
    this.reqType = args.reqType ? args.reqType : null;
    this.reqUrl = args.reqUrl ? args.reqUrl : null;
    this.required = args.required ? true : false;
    this.error = args.error ? args.error : null;

    this.elem = document.getElementById(this.id);
    this.value = this.elem.value;

    this.elem.addEventListener('blur', this, false);
    this.elem.addEventListener('focus', this, false);
}

// FormTitle is the specific field like a text field. There could be many of them.
function FormTitle(args) {
    Field.call(this, args);
}

Field.prototype.getValue = function() { return Helpers.trim( this.value ) };

Field.prototype.blur = function (value) {
    alert("blur");  
};

Field.prototype.focus = function (value) {
    alert("focus");  
};

Field.prototype.handleEvent = function(event) {
    var prop = event.type;
    if ((prop in this) && typeof this[prop] == "function")
        this[prop](this.value);
};

inheritPrototype(FormTitle, Field);
var title = new FormTitle({name: "sa", id: "title"});

function inheritPrototype(e, t) {
    var n = Object.create(t.prototype);
    n.constructor = e;
    e.prototype = n
}

if (!Object.create) {
    Object.create = function (e) {
        function t() {}
        if (arguments.length > 1) {
            throw new Error("Object.create implementation only accepts the first parameter.")
        }
        t.prototype = e;
        return new t
   }
}