document.getElementsByName和document.getElementById 在IE与FF中不同实现

 更新时间:2008年12月19日 13:19:15   作者:  
今天在<asp:radiobuttonlist/>中使用教本的的时候才注意到原来 document.getElementsByName 、document.getElementById 在IE与FF中有着不同实现。
对于ID & Name 按最经典的解释的:“ID 就如同我们的身份证,Name就如同我们的名字”,也就是说,在一个html文档中ID是唯一的,但是Name是可以重复的,就象我们的人名可以重复但是身份证确实全中国唯一的(PS:据说有重复的^_^)
但是对于document.getElementsByName 与document.getElementById 这个两个方法,IE中是并没有严格区分 ID 与 Name 的,比如:
<script type="text/javascript">
function useGetElementsByNameWithId(id) {
var eles = document.getElementsByName('ID_A');
var msg = '使用 getElementsByName 传入 ID:得到:'
if(eles.length > 0) {
msg += " Name " + eles[0].id;
}
alert(msg);
}
function usegetElementByIdWithName(name) {
var ele = document.getElementById(name);
var msg = '使用 getElementById 传入 Name 得到:';
if(ele) {
msg += " ID " + ele.id;
}
alert(msg);
}
</script><input id="ID_A" name="Name_A" type="button" value="使用 getElementsByName 传入 ID" onclick="useGetElementsByNameWithId(this.id)" />
<input id="ID_B" name="Name_B" type="button" value="使用 getElementsByName 传入 Name" onclick="usegetElementByIdWithName(this.name)" />IE中通过 getId 传入 name 同样可以访问到目标元素,而通过 getName 传入 Id 也可以访问到目标元素。
MSDN中对两个方法的解释:
getElementById Method
--------------------------------------------------------------------------------
Returns a reference to the first object with the specified value of the ID attribute.
Remarks
When you use the getElementsByName method, all elements in the document that have the specified NAME or ID attribute value are returned.
Elements that support both the NAME and the ID attribute are included in the collection returned by the getElementsByName method, but not elements with a NAME expando.
MSDN确实对 getElementsByName 方法做了说明:“具有指定 Name 或者 ID 属性的元素都会返回”,但是
getElementById 方法却没有说明,然而内部实现同 getElementsByName 是一样的。
而对于FF,看来更忠实W3C标准,上面的测试代码是没有办法返回目标元素的。
W3C 中的相关信息:
http://www.w3.org/TR/2000/WD-DOM-Level-1-20000929/level-one-html.html#ID-26809268
由于有这个问题,所以对ASP.NET 控件中诸如 radiobuttonlist checkboxlist,使用客户端脚本通过getElementsByName访问具有name属性的成组对象时就要注意了,因为radiobuttonlist 默认会呈现一个table来包容这些radio,而这个table id 与这些radio的name时相同的,比如:
.aspx
<asp:radiobuttonlist id="RadioButtonList1" runat="server">
<asp:listitem>opt1</asp:listitem>
<asp:listitem>opt2</asp:listitem>
<asp:listitem>opt3</asp:listitem>
</asp:radiobuttonlist>HTML:
<table id="RadioButtonList1" border="0">
<tr>
<td><input id="RadioButtonList1_0" type="radio" name="RadioButtonList1" value="opt1" /><label for="RadioButtonList1_0">opt1</label></td>
</tr><tr>
<td><input id="RadioButtonList1_1" type="radio" name="RadioButtonList1" value="opt2" /><label for="RadioButtonList1_1">opt2</label></td>
</tr><tr>
<td><input id="RadioButtonList1_2" type="radio" name="RadioButtonList1" value="opt3" /><label for="RadioButtonList1_2">opt3</label></td>
</tr>
</table>
在IE中使用 document.getElementsByName('RadioButtonList1') 就是返回四个元素了,第一个元素是那个id为 RadioButtonList1 的table,
如果客户端需要有这样的script,也为代码的跨浏览器带来了的麻烦。
注:radiobuttonlist可以选择“流布局”呈现,同样会生成一个类似的外围<span/>来做为容器,也会产生这个问题。

相关文章

  • 基于.net4.0实现IdentityServer4客户端JWT解密

    基于.net4.0实现IdentityServer4客户端JWT解密

    这篇文章主要为大家详细介绍了基于.net4.0实现IdentityServer4客户端JWT解密,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-09-09
  • IIS故障(Connections_Refused)问题分析及处理

    IIS故障(Connections_Refused)问题分析及处理

    这几天某地市Web服务器连续多次出现故障问题(Connections_Refused),正好借这个案例向大家详细介绍下,需要了解的朋友可以参考下
    2012-12-12
  • .NET Core API CORS的实现

    .NET Core API CORS的实现

    这篇文章主要介绍了.NET Core API CORS的实现,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-08-08
  • 使用Lucene.NET实现站内搜索

    使用Lucene.NET实现站内搜索

    提到Lucene,想必大家都有所耳闻,已经是数年前就出现的开源技术。很多站点都是利用它搭建自己网站的站内搜索。由于最近也在做数据检索方面的东西,也学习了下Lucene.net的使用。
    2015-06-06
  • Web.Config文件配置之限制上传文件大小和时间的属性配置

    Web.Config文件配置之限制上传文件大小和时间的属性配置

    在Web.Config文件中配置限制上传文件大小与时间字符串时,是在httpRuntime httpRuntime节中完成的,需要设置以下2个属性:maxRequestLength属性与ExecutionTimeout属性,感兴趣的朋友可以了解下,或许对你有所帮助
    2013-02-02
  • asp.net获取select值的方法

    asp.net获取select值的方法

    今天有个朋友问我如何使用asp.net获取select值,以为很简单的问题,结果发现自己也不知道于是搜索中发现了下面的这个不错的例子,在此与大家分享
    2013-09-09
  • ASP.NET中readonly与const的区别详解

    ASP.NET中readonly与const的区别详解

    如果你学过ASP.NET理论知识都会知道,在ASP.NET中 readonly和const修饰的变量都是恒量,它们的值是不可以被修改的。但是他们之间到底有什么区别?下面小编就它们的区别用例子来进行说明。
    2015-10-10
  • ASP.NET 使用application与session对象写的简单聊天室程序

    ASP.NET 使用application与session对象写的简单聊天室程序

    写了快一年的asp.net,application对象还真没怎么用过。看了看书,根据这两个对象的特性写了一个简单的聊天室程序。真的是非常的简陋
    2014-07-07
  • 编译 dotnet和aspnetcore 源代码详情

    编译 dotnet和aspnetcore 源代码详情

    这篇文章主要介绍了编译 dotnet和aspnetcore 源代码详情,围绕主题相关资料展开展开全文内容,具有一定的参考价值需要的小伙伴可以参考一下
    2022-03-03
  • .NET使用CsvHelper快速读取和写入CSV文件的操作方法

    .NET使用CsvHelper快速读取和写入CSV文件的操作方法

    在日常开发中使用CSV文件进行数据导入和导出、数据交换是非常常见的需求,今天我们来讲讲在.NET中如何使用CsvHelper这个开源库快速实现CSV文件读取和写入,需要的朋友可以参考下
    2024-06-06

最新评论