简体   繁体   English

在IE上,document.getElementsByName不起作用

[英]On IE document.getElementsByName won't work

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? 该怎么办?

Well, the problem is this: IE understands document.getElementsByName(...)[0] as document.getElementById(...). 嗯,问题是:IE将document.getElementsByName(...)[0]理解为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! 因此,如果你还要为你的元素定义一个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必须始终以char开头,所以你必须使用:

<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 CSS file. 同样重要的是,我们可以在CSS文件中通过#a234 {...}来解析每个id。 So we can define an own style for every id , and this makes the id even more powerful. 所以我们可以为每个id定义一个自己的样式 ,这使得id更加强大。

Using getElementsByName to get a DOM Element where the name attribute is not part of the W3C spec (eg, in the question, name doesn't exist for DIV element), IE doesn't get those elements. 使用getElementsByName获取DOM元素,其中name属性不是W3C规范的一部分(例如,在问题中,DIV元素不存在名称),IE不会获取这些元素。 FF does it. FF做到了。
Just to clarify: expando attribute or better known as custom attribute is what I am talking about attributes that are not part of the W3C spec. 只是为了澄清: expando属性或更好地称为自定义属性是我所说的不属于W3C规范的属性。

Read: getElementsByName in IE7 阅读: IE7中的getElementsByName
Read: http://msdn.microsoft.com/en-us/library/ms536438(VS.85).aspx 阅读: http//msdn.microsoft.com/en-us/library/ms536438(VS.85).aspx

So in conclusion: 总之:
Use getElementsByName when trying to get "Form Controls" (input, select, textarea) because they have name as an attribute according to the spec. 在尝试获取“表单控件” (输入,选择,textarea)时使用getElementsByName ,因为它们根据规范将名称作为属性。
If the elements are not Form Controls, use getElementById instead. 如果元素不是表单控件,请改用getElementById。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM