您的位置:首页 > Web前端 > JavaScript

javascript createElement()创建input不能设置name属性的解决方法

2009-10-11 00:00 931 查看
其中obj.name = "select";这句话并不能起到预期的作用,无论是看生成后的HTML代码还是观察使用document.getElementsByName()获取的数组长度,都会发现这个name设置并没有起作用。当然这种情况只出现在IE6和IE7中,而在FireFox中是没有问题的。


 
  Test
 
 
  
  
  
  
   function addInput(){
     var div = document.getElementById("testDiv");
     var obj = document.createElement("input");
     obj.style.width = 63 + "px";
     obj.style.height = 46 + "px";
     obj.name = "select";
     obj.setAttribute("id","button1");
     document.getElementById("testDiv").appendChild(div);
   }
   function show(){
    alert(document.getElementsByName("select").length);
   }
  
 


[Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]
  当然解决方法很简单,在创建时,使用document.createElement("<input name='select'>")。不过还不知道是什么原因?
  在Name属性的说明中有这样一句话:
In Internet Explorer 5, the name property cannot be set at run time on anchor objects dynamically created with the createElement method. To create an anchor with a NAME attribute, include the attribute and value when using the createElement method, or use the innerHTML property.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐