Search

9/04/2007

set name attribute in IE

今天遇到了一個很怪的bug,查到最後是一個動態產生的input沒有name的attribute,不論是用input.name = 'foo'; 或者 input.setAttribute('name', 'foo');都不會work,查了很久才發現這一篇文章,基本上算是個bug:
Setting the “name” attribute in Internet Explorer

The NAME attribute cannot be set at run time on elements dynamically created with the createElement method. To create an element with a name attribute, include the attribute and value when using the createElement method.

function createNamedElement(type, name) {
var element = null;
// Try the IE way; this fails on standards-compliant browsers
try {
element = document.createElement('<'+type+' name="'+name+'">');
} catch (e) {
}
if (!element || element.nodeName != type.toUpperCase()) {
// Non-IE browser; use canonical method to create named element
element = document.createElement(type);
element.name = name;
}
return element;
}

沒有留言: