Failed to set event handler in javascript

时光怂恿深爱的人放手 提交于 2020-01-06 19:32:42

问题


This question is a follow up of post Failed to set event handler in 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() is defined previously. But when I input something in the box and move the focus to other control, the checkName() isn't executed.

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

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 code

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

Is there anyone can help me? Thanks a lot.


回答1:


Try

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


来源:https://stackoverflow.com/questions/4422093/failed-to-set-event-handler-in-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!