IE7 中的 getElementsByName [英] getElementsByName in IE7

查看:25
本文介绍了IE7 中的 getElementsByName的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些代码这样做:

 var changes = document.getElementsByName(from);for (var c=0; c

这在 FF 和 Chrome 中工作正常,但在 IE7 中无效.大概是因为 getElementsByName 在 IE 中不起作用.最好的解决方法是什么?

解决方案

如果你不知道为什么这在 IE 中不起作用,这里是 有关该功能的 MSDN 文档:

<块引用>

当您使用 getElementsByName 方法时,将返回文档中具有指定 NAME 属性或 ID 属性值的所有元素.

同时支持 NAME 属性和 ID 属性的元素包含在 getElementsByName 方法返回的集合中,但具有 NAME expando 的元素不包含在集合中;因此,此方法不能用于按名称检索自定义标签.

Firefox 允许 getElementsByName() 检索使用 NAME expando 的元素,这就是它起作用的原因.这是否是一件好事™可能还有待商榷,但这就是现实.

因此,一种选择是使用 getAttribute() DOM 方法请求 NAME 属性,然后测试该值以查看它是否是您想要的,如果是,则将其添加到数组中.但是,这需要您迭代页面中的所有节点或至少在一个小节内迭代,这不是最有效的.您可以使用类似 getElementsByTagName 之类的内容预先限制该列表() 也许.

如果您控制页面的 HTML,另一种方法是为所有感兴趣的元素提供一个仅随数字变化的 ID,例如:

...

<div id="Change1">...</div><div id="Change2">...</div><div id="Change3">...</div>

然后像这样使用 JavaScript:

//假设连续编号,从 0 开始函数 getElementsByModifiedId(baseIdentifier) {var allWantedElements = [];var idMod = 0;while(document.getElementById(baseIdentifier + idMod)) {//找不到就停止allWantedElements.push(document.getElementById(baseIdentifier + idMod++));}返回所有想要的元素;}//像这样调用它:var changes = getElementsByModifiedId("Change");

这当然是一种 hack,但它可以完成您需要的工作,并且与其他一些 hack 相比效率不会太低.

如果您正在使用某种 JavaScript 框架/工具包,那么您的选择要好得多,但除非您表明正在使用,否则我没有时间深入探讨这些细节.就我个人而言,我不知道没有它的人们如何生活,它们节省了太多的时间、精力和挫折,以至于您无法使用它.

I have some code doing this :

 var changes = document.getElementsByName(from);
 for (var c=0; c<changes.length; c++) {
   var ch = changes[c];
   var current = new String(ch.innerHTML);
   etc.
 }

This works fine in FF and Chrome but not in IE7. Presumably because getElementsByName isn't working in IE. What's the best workaround?

解决方案

In case you don't know why this isn't working in IE, here is the MSDN documentation on that function:

When you use the getElementsByName method, all elements in the document that have the specified NAME attribute or ID attribute value are returned.

Elements that support both the NAME attribute and the ID attribute are included in the collection returned by the getElementsByName method, but elements with a NAME expando are not included in the collection; therefore, this method cannot be used to retrieve custom tags by name.

Firefox allows getElementsByName() to retrieve elements that use a NAME expando, which is why it works. Whether or not that is a Good Thing™ may be up for debate, but that is the reality of it.

So, one option is to use the getAttribute() DOM method to ask for the NAME attribute and then test the value to see if it is what you want, and if so, add it to an array. This would require, however, that you iterate over all of the nodes in the page or at least within a subsection, which wouldn't be the most efficient. You could constrain that list beforehand by using something like getElementsByTagName() perhaps.

Another way to do this, if you are in control of the HTML of the page, is to give all of the elements of interest an Id that varies only by number, e.g.:

<div id="Change0">...</div>
<div id="Change1">...</div>
<div id="Change2">...</div>
<div id="Change3">...</div>

And then have JavaScript like this:

// assumes consecutive numbering, starting at 0
function getElementsByModifiedId(baseIdentifier) {
    var allWantedElements = [];
    var idMod = 0;
    while(document.getElementById(baseIdentifier + idMod)) { // will stop when it can't find any more
        allWantedElements.push(document.getElementById(baseIdentifier + idMod++));
    }
    return allWantedElements;
}

// call it like so:
var changes = getElementsByModifiedId("Change");

That is a hack, of course, but it would do the job you need and not be too inefficient compare to some other hacks.

If you are using a JavaScript framework/toolkit of some kind, you options are much better, but I don't have time to get into those specifics unless you indicate you are using one. Personally, I don't know how people live without one, they save so much time, effort and frustration that you can't afford not to use one.

这篇关于IE7 中的 getElementsByName的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆