用于特定用途的getElementsByClassName的Polyfill [英] Polyfill for getElementsByClassName for particular uses
问题描述
如何使this.getElementsByClassName('class')[0]
在Internet Explorer 6-8中工作?是否有任何polyfill可以解决此问题?
How can I make this.getElementsByClassName('class')[0]
work for Internet Explorer 6-8? Is there any polyfill to fix this?
推荐答案
仅作记录,较旧的浏览器仍然存在,因为人们不断努力支持它们.
Just for the record, older browsers are still alive because people keep making efforts to support them.
为document.getElementsByClassName填充
话虽如此,但简短的Google搜索可能已将您带到此链接: https://gist.github.com/eikes/2299607
With that said, a short google search could have brought you to this link: https://gist.github.com/eikes/2299607
用于IE6/7的polyfill是这样的:
The polyfill for IE6/7 is like this:
if (d.evaluate) { // IE6, IE7
pattern = ".//*[contains(concat(' ', @class, ' '), ' " + search + " ')]";
elements = d.evaluate(pattern, d, null, 0, null);
while ((i = elements.iterateNext())) {
results.push(i);
}
}
基于document.evaluate
方法
https://developer.mozilla.org/en -US/docs/Web/API/document.evaluate
为element.getElementsByClassName
您似乎想在HTML元素而不是文档上调用getElementsByClassName方法.不幸的是,我认为您不能在IE6和7(甚至8)上进行填充,因为此答案似乎表明: 如何将我自己的方法添加到HTMLElement对象?
You seem to want to call the getElementsByClassName method on a HTML element instead of on the document. Unfortunately i don't think you can polyfill that on IE6 and 7 (and even 8), as this answer seems to suggest: How to add my own methods to HTMLElement object?
您仍然可以使用document.evaluate
完成所需的功能(提示:第二个参数是一个上下文节点;它应该是您的元素),但是您需要将调用代码更改为以下内容:
You can still use document.evaluate
to acomplish the functionality you want (hint: the second parameter is a context node; it should be your element), but you need to change the calling code to something like this:
<div onclick="myPolyfill('class', this)[0].innerHTML = 'works'">
这篇关于用于特定用途的getElementsByClassName的Polyfill的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!