Javascript 在 IE document.getElementsByName 上不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14575671/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
On IE document.getElementsByName won't work
提问by Marcus
I use this code:
我使用这个代码:
<div name="1234">
<img src="pic.gif" height="70" width="100" onMouseOver="clear('1234')">
</div>
And:
和:
function clear(element_name){
document.getElementsByName(element_name)[0].innerHTML="";
}
It does work in Firefox and Opera, but doesn't work in IE 6.0 or IE 8.0, and probably not even in newer IE's.
它在 Firefox 和 Opera 中确实有效,但在 IE 6.0 或 IE 8.0 中不起作用,甚至可能在较新的 IE 中也不起作用。
What to do?
该怎么办?
采纳答案by Marcus
Well, the problem is this: IE understands document.getElementsByName(...)[0] as document.getElementById(...). So if you would define also an id for your element, the method document.getElementsByName(element_name)[0].innerHTML="" will surprisingly also work in IE!
好吧,问题是这样的:IE 将 document.getElementsByName(...)[0] 理解为 document.getElementById(...)。因此,如果您还要为元素定义一个 id,那么方法 document.getElementsByName(element_name)[0].innerHTML="" 出人意料地也可以在 IE 中工作!
But since you anyway need to define an id due to IE, and since an id must always start with a char first, you must use:
但是由于 IE 的原因,您无论如何都需要定义一个 id,并且 id 必须始终以字符开头,因此您必须使用:
<div id="a234">
<img src="pic.gif" height="70" width="100" onMouseOver="clear('a234')">
</div>
And this command:
而这个命令:
function clear(element_id){
document.getElementById(element_id).innerHTML="";
}
Even more, document.getElementsByName(...)[0] is slower in Firefox: http://www.uize.com/tests/performance/getElementById-vs-getElementsByName.html
更重要的是,document.getElementsByName(...)[0] 在 Firefox 中更慢:http: //www.uize.com/tests/performance/getElementById-vs-getElementsByName.html
So the id definitely wins the race.
所以id肯定会赢得比赛。
UPDATE:
更新:
Also important is the fact, that we can adress every id by #a234{...} in a CSSfile. So we can define an own style for every id, and this makes the id even more powerful.
同样重要的是,我们可以通过 #a234{...} 在CSS文件中对每个 ID 进行寻址。所以我们可以为每个 id定义一个自己的样式,这使得 id 更加强大。
回答by Esteban
Using getElementsByNameto get a DOM Element where the nameattribute is not part of the W3C spec (eg, in the question, name doesn't exist for DIV element), IE doesn't get those elements. FF does it.
Just to clarify: expando attributeor better known as custom attributeis what I am talking about attributes that are not part of the W3C spec.
使用getElementsByName获取名称属性不属于 W3C 规范的一部分的DOM 元素(例如,在问题中,DIV 元素不存在名称),IE 无法获取这些元素。FF做到了。
澄清一下:expando 属性或更广为人知的自定义属性是我所说的不属于 W3C 规范的属性。
Read: getElementsByName in IE7
Read: http://msdn.microsoft.com/en-us/library/ms536438(VS.85).aspx
阅读:IE7 中的 getElementsByName
阅读:http: //msdn.microsoft.com/en-us/library/ms536438 (VS.85) .aspx
So in conclusion:
Use getElementsByNamewhen trying to get "Form Controls"(input, select, textarea) because they have nameas an attribute according to the spec.
If the elements are not Form Controls, use getElementById instead.
所以结论是:在尝试获取“表单控件”(输入、选择、文本区域)时
使用getElementsByName,因为它们根据规范将名称作为属性。
如果元素不是表单控件,请改用 getElementById。

