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

所属分类: 网络编程 / ASP.NET 阅读数: 1379
收藏 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二级域名共享Forms身份验证、下载站/图片站的授权访问控制

我们平时一般在做图片或者文件下载权限控制的时候基本都是控制到下载页面的,当你的下载地址暴露后,浏览者就直接可以通过文件地址进行下载了,这时候也就出现了我们常说的盗链
收藏 0 赞 0 分享

在ASP.NET中下载文件的实现代码

通过ASP.NET来下载文件,这个问题可大可小,我们先从小的开始。当我们要让用户下载一个文件
收藏 0 赞 0 分享

asp.net下日期和时间处理的类库

发一个专门处理时间和日期的类库,记录以备查询
收藏 0 赞 0 分享

LINQ重写博客垃圾图片回收算法

本人博客后台管理模块有个功能,可以扫描图片上传文件夹下所有未被引用的博客
收藏 0 赞 0 分享

C#多线程Singleton(单件)模式模板

下面是一个C#多线程单件模式的代码模板。把T换成你自己的类型就可以使用了。其精妙之处就在于用lock语句锁定资源来避免多线程同时走入if语句去创建多个对象
收藏 0 赞 0 分享

URL重写及干掉ASP.NET试图状态的实现方法

URL重写已经很普遍了,但基本上大部分的URL重写都不支持页面的相对路径,所有如果想在已经开发好的项目中添加还是有压力的,第二就是例如微软的那个URL重写是根据正则表达式来处理的,那样是很好,但也有不足之处,就是不方便定位到某个页面只能有哪些参数
收藏 0 赞 0 分享

正则方式的自动小偷抓网程序

公司里面有许多数据没人去录入,做一个抓取网页的程序,以前做CMS系统的时候涉及过,不过这次的处理HTML上和以前做了些区别
收藏 0 赞 0 分享

asp.net生成缩略图实现代码

此文件imgSmall.ashx专门用来生成图片的缩略图,可以减少服务器压力,降低网络流量,初学者必备
收藏 0 赞 0 分享

asp.net richTextBox中高亮显示选中字符串或文本

最近开发程序需要对一段文本中的某个字符串进行高亮显示,网上找了下资料
收藏 0 赞 0 分享

ASP.net的验证控件浅析

前些天在做注册页面的验证的时候,用了下ASP.net的验证控件,有一些体会,特写下这篇博客,如果有朋友有不同ideas,欢迎大家留言
收藏 0 赞 0 分享
查看更多