我想對焦點事件添加一個文本框如何將焦點等事件添加到在javascript中使用document.createElement()創建的元素?
var el = document.createElement('input');
el.type = 'text';
el.setAttribute("id",'suggest22');
我想對焦點事件添加一個文本框如何將焦點等事件添加到在javascript中使用document.createElement()創建的元素?
var el = document.createElement('input');
el.type = 'text';
el.setAttribute("id",'suggest22');
var el = document.createElement('input');
el.type = 'text';
el.setAttribute('id','suggest22');
el.onclick = function(){alert(this.id);}
el.onfocus = function() {
// Focused.
}
您也可以在符合標準的瀏覽器中使用addEventListener('focus', function() { ... }, false)
,在< IE9中使用attachEvent('onfocus', function() { ... })
。
此外,您可以使用id
屬性作爲設置元素的id
屬性的快捷方式。
你也不應該混合單('
)和雙("
)。始終如一地使用其中一個。
1用於建議符合標準規範的替代方案。 (*'attachEvent'版本應該在**上有前綴**,儘管對於事件名稱是'onfocus') – 2011-05-21 09:22:13
@Gaby Cheers,我已經記住了並修復了它:) – alex 2011-05-21 09:23:24
phew ...我看到了更正版本,不知道你是否編輯過或我第一次誤讀它:) – 2011-05-21 09:24:39