如何将焦点事件之类的事件添加到javascript中使用document.createElement()创建的元素中?

如何将焦点事件之类的事件添加到javascript中使用document.createElement()创建的元素中?,javascript,html,javascript-events,Javascript,Html,Javascript Events,我想在文本框中添加聚焦事件 var el = document.createElement('input'); el.type = 'text'; el.setAttribute("id",'suggest22'); 您还可以在符合标准的浏览器中使用addEventListener('focus',function(){…},false),在

我想在文本框中添加聚焦事件

 var el = document.createElement('input');
           el.type = 'text';

             el.setAttribute("id",'suggest22');  

您还可以在符合标准的浏览器中使用
addEventListener('focus',function(){…},false)
,在attachEvent('onfocus',function(){…})

此外,还可以使用
id
属性作为设置元素的
id
属性的快捷方式

你也不应该把单(
)和双(
)混在一起。要始终如一地使用其中一种

var el = document.createElement('input');
el.type = 'text';
el.setAttribute('id','suggest22');

el.onclick = function(){alert(this.id);} 

demo

+1,用于建议符合标准的备选方案。(虽然
附件事件
版本应在事件名称前加上前缀,
onfocus
)呸…我看到了更正的版本,不确定您是否编辑过或第一次误读过:)
var el = document.createElement('input');
el.type = 'text';
el.setAttribute('id','suggest22');

el.onclick = function(){alert(this.id);}