【问题标题】:How to make document.querySelector work in IE6如何使 document.querySelector 在 IE6 中工作
【发布时间】:2015-03-27 12:03:36
【问题描述】:

我在一个网站上工作,但我得到了一个在 Internet Explorer 6 中不起作用的 javascript 函数。 我知道

document.querySelector(selector)

只工作in Internet Explorer 8+

我能做些什么让它在 IE6 中工作?

(我不会为了好玩而让它工作;))

【问题讨论】:

  • 你为什么要这样折磨自己?
  • 请不要。让它在燃烧的地狱中沉沦……它会遇到它的命运。我听说撒旦正在等待 IE6
  • 在 BW 显示器上获取颜色...
  • 如果客户需要它(我认为是,你不会为了好玩而这样做),你必须对它进行 pollyfill ,如果需要旧版支持,甚至可以使用 jquery 之类的东西。 (或者如果它只是你想要的选择,也许只是使用嘶嘶声)
  • 如果客户在 ie6 上拥有大量用户(上帝保佑),那么他们应该要求 ie6 兼容性就更可接受了

标签: javascript internet-explorer-6


【解决方案1】:

您可以使用 polyfill,例如 one,但仍然使用 IE6,这是 IT 中死灵术的类似物。

上面提到的polyfill是基于polyfill.js,可以在here找到,它为很多ECMA 5函数提供了polyfills。

我将发布脚本的当前状态,也许它在将来会有用(虽然我真的希望,它不会是:)):

if (!document.querySelectorAll) {
    document.querySelectorAll = function (selectors) {
        var style = document.createElement('style'), elements = [], element;
        document.documentElement.firstChild.appendChild(style);
        document._qsa = [];

        style.styleSheet.cssText = selectors +
                '{x-qsa:expression(document._qsa && document._qsa.push(this))}';
        window.scrollBy(0, 0);
        style.parentNode.removeChild(style);

        while (document._qsa.length) {
            element = document._qsa.shift();
            element.style.removeAttribute('x-qsa');
            elements.push(element);
        }
        document._qsa = null;
        return elements;
    };
}

if (!document.querySelector) {
    document.querySelector = function (selectors) {
        var elements = document.querySelectorAll(selectors);
        return (elements.length) ? elements[0] : null;
    };
}

【讨论】:

  • 你现在提到的更新后的polyfill可以在here找到
【解决方案2】:

我强烈建议您不要再尝试支持 IE6。


但您可以使用this very clever trick from an Ajaxian article 添加document.querySelector 和document.querySelectorAll。这篇文章实际上有点错误,它添加了一个名为 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;
    }
@*/

虽然我从不支持这样使用for-in; details and alternatives in this other answer.

据推断,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 或Element#querySelectorAll(仅在元素内查看的版本),只是添加了document.querySelector 和document.querySelectorAll。而且你不能在 IE6 上添加 Element 版本而不将它们添加到每个单独的元素,因为 IE6 不支持元素原型。

【讨论】:

  • @AmitJoki: :-) 不是我,我只记得几年前读过一个技巧,通过搜索很快就找到了。
  • 实际上,在某些业务领域(例如计算机维修公司)仍然支持 IE 6 是件好事,这样使用 IE6 的人可以通过该网站知道支付费用来升级他们的浏览器(可能还有他们的OS),因为对于某些人来说,他们能够使用鼠标只是一个奇迹。
  • HI T.J,我正在尝试在我的项目中使用您上面的代码。不过我遇到了一些麻烦。你能看看我的问题,看看你的想法吗? stackoverflow.com/questions/53539726/…
猜你喜欢
  • 2022-10-01
  • 1970-01-01
  • 2022-11-20
  • 1970-01-01
  • 2010-12-12
  • 1970-01-01
  • 2010-12-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多