无法在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);
}