getElementsByName在IE7中 [英] getElementsByName in IE7

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

问题描述

我有一些代码这样做:

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

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

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

推荐答案

如果你不知道为什么这不工作在IE,这里是该功能的MSDN文档

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


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

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

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

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允许 getElementsByName() 来检索使用的元素一个名称扩展,这是为什么它的作品。这是否是一个好事物可能会被争议,但这是现实。

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.

所以,一个选项是使用 getAttribute() DOM方法来查询NAME属性,然后测试该值以查看它是否是您想要的,如果是,则将其添加到数组中。然而,这将需要您遍历页面中的所有节点,或至少在一个小节内,这将不是最有效的。您可以通过使用 getElementsByTagName() 也许。

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.

另一种方法来做到这一点,如果你在控制页面的HTML ,是要给所有感兴趣的元素,这个id只能由数字变化,例如:

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>

然后有这样的JavaScript:

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.

如果您使用某种JavaScript框架/工具包,您的选项会更好,但我没有时间进入那些细节,除非你指出你正在使用一个。就个人而言,我不知道人们如何生活,没有一个人,他们节省了如此多的时间,努力和挫折,你买不起使用一个。

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.

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

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