无法在javascript中设置事件处理程序 [英] Failed to set event handler in javascript

查看:107
本文介绍了无法在javascript中设置事件处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题是后续在JavaScript中设置事件处理程序的后续操作

我想为输入文本控件添加一个事件处理程序。输入框控件是动态生成的。我的代码如下:

I want to add an event handler for input text controls. The input box control is generated dynamically. My code is like:

_inputbox = document.createElement("input");
_inputbox.type = "text";
_inputbox.id = settings[zindex];
_inputbox.onblur = checkName; 

以前定义了checkName()。但是当我在框中输入内容并将焦点移动到其他控件时,checkName()不会执行。

checkName() is defined previously. But when I input something in the box and move the focus to other control, the checkName() isn't executed.

在Firebug的DOM选项卡中, onblur被正确地分配给checkName()。

In the DOM tab of Firebug, I find the onblur is assigned to checkName() correctly.

在Firebug的HTML选项卡中,我发现输入框只定义了一个ID和类型。没有onblur的HTML代码。如果我编辑HTML并手动添加onblur = checkName()。该功能可以被成功调用。

In the HTML tab of Firebug, I find the input box only defines an "ID" and a "type". No "onblur" in its HTML code. If I edit the HTML and add onblur=checkName() manually. The function can be called successfully.

HTML代码

<input type="text" id="Datastore">

有没有人可以帮助我?非常感谢。

Is there anyone can help me? Thanks a lot.

推荐答案

尝试

If(_inputbox.addEventListener) {
    _inputbox.addEventListener('blur', checkName, false);
} else { 
    _inputbox.attachEvent('onblur', checkName);
}

这篇关于无法在javascript中设置事件处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆