![](/img/trans.png)
[英]How to add class to element created using document.createElement?
[英]How can we add events like on focus to an element created using document.createElement() in JavaScript?
我想在文本框中添加一个焦点事件:
var el = document.createElement('input');
el.type = 'text';
el.setAttribute("id",'suggest22');
el.onfocus = function() {
// Focused.
}
您还可以在符合标准的浏览器中使用addEventListener('focus', function() {... }, false)
并在 < IE9 中使用attachEvent('onfocus', function() {... })
。
此外,您可以使用id
属性作为设置元素的id
属性的快捷方式。
您也不应该混合使用单 ( '
) 和双 ( "
)。始终使用其中一个。
var el = document.createElement('input');
el.type = 'text';
el.setAttribute('id','suggest22');
el.onclick = function(){alert(this.id);}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.