加入收藏 | 设为首页 | 会员中心 | 我要投稿 河北网 (https://www.hebeiwang.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程 > 正文

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

发布时间:2020-05-13 01:33:12 所属栏目:编程 来源:站长网
导读:对付ID lt;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(
对付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 type="button" value="行使 getElementsByName 传入 ID" />
<input type="button" value="行使 getElementsByName 传入 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 中的相干信息:
#ID-26809268
因为有这个题目,以是对ASP.NET 控件中诸如 radiobuttonlist checkboxlist,行使客户端剧本通过getElementsByName会见具有name属性的成组工具时就要留意了,由于radiobuttonlist 默认会泛起一个table来海涵这些radio,而这个table id 与这些radio的name时沟通的,好比:
.aspx
<asp:radiobuttonlist runat="server">
<asp:listitem>opt1</asp:listitem>
<asp:listitem>opt2</asp:listitem>
<asp:listitem>opt3</asp:listitem>
</asp:radiobuttonlist>HTML:
<table>
<tr>
<td><input type="radio" value="opt1" /><label for="RadioButtonList1_0">opt1</label></td>
</tr><tr>
<td><input type="radio" value="opt2" /><label for="RadioButtonList1_1">opt2</label></td>
</tr><tr>
<td><input type="radio" value="opt3" /><label for="RadioButtonList1_2">opt3</label></td>
</tr>
</table>
在IE中行使 document.getElementsByName('RadioButtonList1') 就是返回四个元素了,第一个元素是谁人id为 RadioButtonList1 的table,
假如客户端必要有这样的script,也为代码的跨赏识器带来了的贫困。
注:radiobuttonlist可以选择“流机关”泛起,同样会天生一个相同的外围<span/>来做为容器,也会发生这个题目。

(编辑:河北网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读