我在一个网站上工作,我得到了一个在 Internet Explorer 6 中不起作用的 JavaScript 函数。
我知道
document.querySelector(selector)
只工作在 Internet Explorer 8+ 中
我该怎么做才能使其在 IE6 中运行?
(我并不是为了好玩而试图让它发挥作用;))
我强烈建议您不要再尝试支持 IE6。
但你可以添加document.querySelector
and document.querySelectorAll
using 这是阿贾克斯文章中的一个非常聪明的技巧。这篇文章实际上有点错误,它添加了一个叫做querySelector
确实如此querySelectorAll
反而。我在这里修正了这个名字:
/*@cc_on
if (!document.querySelectorAll)
document.querySelectorAll = function(selector)
{
var head = document.documentElement.firstChild;
var styleTag = document.createElement("STYLE");
head.appendChild(styleTag);
document.__qsResult = [];
styleTag.styleSheet.cssText = selector + "{x:expression(document.__qsResult.push(this))}";
window.scrollBy(0, 0);
head.removeChild(styleTag);
var result = [];
for (var i in document.__qsResult)
result.push(document.__qsResult[i]);
return result;
}
@*/
虽然我会never面容使用for-in
像那样;其他答案中的详细信息和替代方案.
并由此推断,querySelector
:
/*@cc_on
if (!document.querySelector)
document.querySelector = function(selector)
{
var head = document.documentElement.firstChild;
var styleTag = document.createElement("STYLE");
head.appendChild(styleTag);
document.__qsResult = [];
styleTag.styleSheet.cssText = selector + "{x:expression(document.__qsResult.push(this))}";
window.scrollBy(0, 0);
head.removeChild(styleTag);
// Return first result only
return document.__qsResult[0] || null;
}
@*/
请注意,以上都没有添加Element#querySelector
or Element#querySelectorAll
(仅在元素内查看的版本),只是document.querySelector
and document.querySelectorAll
。而且你不能添加Element
IE6 上的版本而不将它们添加到每个单独的元素,因为 IE6 不支持元素原型。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)