代码改变世界

ie与firefox对于type属性值的不同

2009-05-29 21:04  wlstyle  阅读(1037)  评论(0)    收藏  举报

在ie中动态创建input标签并且设置type的属性。在firefox中一切正常ie中就会有问题。

首先看代码:

  var input = document.createElement('input');
            
var attribute = {
                name: 
'files',
                type: 
'file',
                id: 
"J_File"
            }
            
for (var key in attribute) {
                input.setAttribute(key, attribute[key]);
                
            }
            document.body.appendChild(input);

 在这里通过js动态的创建了一个input节点。然后他它追加到body节点的尾部。这里表明在ie中可以在没有实际追加元素的到实际的的dom结构中的时候可以动态设置这个input节点的type值。改变代码 我们在追加之后动态改变这个元素的type值。

 var input = document.createElement('input');
            
var attribute = {
                name: 
'files',
                type: 
'file',
                id: 
"J_File"
            }
            
for (var key in attribute) {
                input.setAttribute(key, attribute[key]);
                
            }
            document.body.appendChild(input);
            alert(input.getAttribute(
'type'));
            input.setAttribute(
'type','text');

 在ie中测试可以得到该元素的type属性的属性值。但是当通过javascript设置该属性的属性值的时候会报错(不支持该命令).在firefox中支持动态改变该元素的type的属性值。说明对于ie这个type属性是一个只读属性。而对于firefox是一个可读写的属性。

总结:ie和firefox都支持在没有将元素置入dom结构中的时候设置type的属性值。而当元素在dom结构中之后ie无法通过js改变该属性值。对于ie这是一个只读属性。对于firefox type属性是一个可读写的属性

注:firefox为最新版本3.0.10。ie为ie6 ie7