document.querySelectorAll 在 IE、Edge 和 Safari 中的兼容性

Compatibility of document.querySelectorAll in IE, Edge and Safari

参考答案:

请参阅 and the answer linked 仅适用于 Firefox、Chrome 和 Opera。

我做了一些研究,发现它(不区分大小写标志)不兼容。我需要 IE、Edge 和 Safari 中的等效项。

var divs = document.querySelectorAll('div[class^="foo" i]');

由于 css 级别 4 仍处于草稿阶段,区分大小写的选择器与大多数浏览器不兼容。您可以使用这样的过滤方法:

var divs = [].slice.call(document.querySelectorAll('div')).filter(function(el){
   return el.className.match(/^foo/i);
});

更新:需要声明您现在可以使用 css4 选择器。

document.querySelectorAll('div[class^="foo" i]');

有关浏览器兼容性,请参阅 this link