javascript document.getElementsByClassName 与 IE 的兼容性
检索具有特定类的元素数组的最佳方法是什么?
我会使用 document.getElementsByClassName 但 IE 不支持它。
所以我尝试了 Jonathan Snook 的解决方案:
function getElementsByClassName(node, classname) {
var a = [];
var re = new RegExp('(^| )'+classname+'( |$)');
var els = node.getElementsByTagName("*");
for(var i=0,j=els.length; i<j; i++)
if(re.test(els[i].className))a.push(els[i]);
return a;
}
var tabs = document.getElementsByClassName(document.body,'tab');
...但 IE 仍然显示:
对象不支持此属性或方法
有什么想法、更好的方法、错误修复吗?
我不想使用任何涉及 jQuery 或其他“庞大的 javascript”的解决方案。
更新:
我让它工作了!
正如 @joe 提到的,该函数不是 document
的方法。
所以工作代码如下所示:
function getElementsByClassName(node, classname) {
var a = [];
var re = new RegExp('(^| )'+classname+'( |$)');
var els = node.getElementsByTagName("*");
for(var i=0,j=els.length; i<j; i++)
if(re.test(els[i].className))a.push(els[i]);
return a;
}
var tabs = getElementsByClassName(document.body,'tab');
...此外如果您只需要 IE8+ 支持那么这将起作用:
if(!document.getElementsByClassName) {
document.getElementsByClassName = function(className) {
return this.querySelectorAll("." + className);
};
Element.prototype.getElementsByClassName = document.getElementsByClassName;
}
像平常一样使用它:
var tabs = document.getElementsByClassName('tab');
What is the best method to retrieve an array of elements that have a certain class?
I would use document.getElementsByClassName but IE does not support it.
So I tried Jonathan Snook's solution:
function getElementsByClassName(node, classname) {
var a = [];
var re = new RegExp('(^| )'+classname+'( |$)');
var els = node.getElementsByTagName("*");
for(var i=0,j=els.length; i<j; i++)
if(re.test(els[i].className))a.push(els[i]);
return a;
}
var tabs = document.getElementsByClassName(document.body,'tab');
...but IE still says:
Object doesn't support this property or method
Any ideas, better methods, bug fixes?
I would prefer not to use any solutions involving jQuery or other "bulky javascript".
Update:
I got it to work!
As @joe mentioned the function is not a method of document
.
So the working code would look like this:
function getElementsByClassName(node, classname) {
var a = [];
var re = new RegExp('(^| )'+classname+'( |$)');
var els = node.getElementsByTagName("*");
for(var i=0,j=els.length; i<j; i++)
if(re.test(els[i].className))a.push(els[i]);
return a;
}
var tabs = getElementsByClassName(document.body,'tab');
...Also if you only need IE8+ support then this will work:
if(!document.getElementsByClassName) {
document.getElementsByClassName = function(className) {
return this.querySelectorAll("." + className);
};
Element.prototype.getElementsByClassName = document.getElementsByClassName;
}
Use it just like normal:
var tabs = document.getElementsByClassName('tab');
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
这不是文档的方法:
It's not a method of document:
您可以为旧版浏览器创建该功能
you may create the function for older browsers
很确定这与 Leonid 的函数相同,但它尽可能使用
document.getElementsByClassName
。Pretty sure this is the same as Leonid's function but this uses
document.getElementsByClassName
when it can.您无法真正复制 getElementsByClassName,因为它返回一个 nodeList,
因此它的值是实时的,并随文档更新。
您可以返回共享相同类名的元素的静态数组 -
但它不会“知道”文档何时更改。
(不需要太多这样的东西就可以使库看起来更简洁...)
//Example
var A= getArrayByClassNames('sideBar local')
You can't really replicate getElementsByClassName, because it returns a nodeList,
and so its value is live, and updates with the document.
You can return a static Array of elements who share the same classnames-
but it won't 'know'when the document changes.
(It won't take too many of these kind of things to make a library look svelte...)
//Example
var A= getArrayByClassNames('sideBar local')
IE8:
IE8:
我只是想改进 IE8 的
querySelectorAll
后备。就像其他人回答的那样,简单的方法是将函数添加到 Element.prototype 中,
但存在一些问题:
/
、$
、*
等)。这意味着应该有一些“修复”,例如:
代码:
I just want to improve
querySelectorAll
fallback for IE8.Like others answered, the simple way is adding the function to
Element.prototype
withBut there are some problems:
/
,$
,*
, etc.)That means there should be some "fixing", for example:
Code: