当前位置: 首页 > 新闻动态 > 软件编程

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

作者:用户投稿 浏览: 发布日期:2026-01-16
[导读]:今天在使用document.createElement()动态创建input时,发现不能为其name赋值,以下是测试代码。
其中obj.name = "select";这句话并不能起到预期的作用,无论是看生成后的HTML代码还是观察使用document.getElementsByName()获取的数组长度,都会发现这个name设置并没有起作用。当然这种情况只出现在IE6和IE7中,而在FireFox中是没有问题的。

[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.
免责声明:转载请注明出处:http://jing-feng.com.cn/news/25167.html

扫一扫高效沟通

多一份参考总有益处

免费领取网站策划SEO优化策划方案

请填写下方表单,我们会尽快与您联系
感谢您的咨询,我们会尽快给您回复!