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

所属分类: 网络编程 / ASP.NET 阅读数: 1417
收藏 0 赞 0 分享
对于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/>来做为容器,也会产生这个问题。
更多精彩内容其他人还在看

ASP.NET笔记之 控件与母板的区别分析

本篇文章小编为大家介绍,ASP.NET笔记之 控件与母板的区别分析。需要的朋友参考下
收藏 0 赞 0 分享

.NET动态加载用户控件并传值的方法

.NET动态加载用户控件并传值的方法,有需要的朋友可以参考一下
收藏 0 赞 0 分享

扩展ASP.NET MVC三层框架且使用StructureMap实现依赖注入1-Model层

本篇文章将向大家介绍如何添加Service和Repository层并且使用StructureMap把Service层注入到Controller,把Repository注入到Service层。
收藏 0 赞 0 分享

aspnetpager重写url(伪静态)配置实例

这几天要用到AspNetPager来做伪静态分页,找了些资料并把修改过程记录下来。
收藏 0 赞 0 分享

C#实现支持断点续传多线程下载客户端工具类

C#实现支持断点续传多线程下载的 Http Web 客户端工具类 (C# DIY HttpWebClient),感兴趣的朋友可以参考下本文,或许对你有所帮助
收藏 0 赞 0 分享

DataGridView多维表头的实现方法

不过我自己还是扩展了DataGridView,使之能制作出多维表头。
收藏 0 赞 0 分享

gridview+objectdatasource+aspnetpager整合实例

gridview+objectdatasource+aspnetpager整合实例,需要的朋友可以参考一下
收藏 0 赞 0 分享

DataTable多列合并问题轻松搞定

由于题库的表结构不相同,导致同样的Gridview在显示时不能同时两种不同结构的数据,这时如何在这个固定的GridView中显示不同的数据呢,感兴趣的朋友可以看下本文的解决方法
收藏 0 赞 0 分享

Asp.net静态方法之Grid转DataTable方法实现步骤

GridView绑定DataTable后,如何获取GridView绑定后显示的值,在项目需求的背景下写了一个静态方法,经过在项目中的使用,bug的修复,较为稳定
收藏 0 赞 0 分享

Asp.net防重复提交机制实现方法

在Button或其他控件加上下面两个属性:UseSubmitBehavior="false"及OnClientClick设置控件为不可用即可,感兴趣的朋友可以参考下哈
收藏 0 赞 0 分享
查看更多