如何使document.querySelector在IE6中工作 [英] How to make document.querySelector work in IE6

查看:164
本文介绍了如何使document.querySelector在IE6中工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I work on a website and I got a javascript function that doesn't work in Internet Explorer 6. I know

document.querySelector(selector)

仅工作在Internet Explorer 8+中

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

What could I do to make it work in IE6?

(我不试图让它变得有趣;))

(I don't try to make it work for the fun of it ;) )

推荐答案

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

I strongly encourage you not to try to support IE6 any longer.

但您可以使用 document.querySelector 和 document.querySelectorAll / archives / creating-a-queryselector-for-ie-that-run-at-native-speedrel =noreferrer>来自Ajaxian文章的这个非常聪明的技巧。这篇文章实际上有点不对,它添加了一个名为 querySelector 的东西来代替 querySelectorAll 。我在这里修改了这个名字:

But you can add document.querySelector and document.querySelectorAll using this very clever trick from an Ajaxian article. The article actually gets it a bit wrong, it adds something called querySelector that does querySelectorAll instead. I've fixed the name here:

/*@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 之类的; 其他答案中的详细信息和替代方案

Although I would never countenance using for-in like that; 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 (仅在element),只需 document.querySelector document.querySelectorAll 。并且你不能在IE6上添加元素版本而不将它们添加到每个单独的元素,因为IE6不支持元素原型。


Note that neither of the above adds Element#querySelector or Element#querySelectorAll (the versions that look only within an element), just document.querySelector and document.querySelectorAll. And you can't add the Element versions on IE6 without adding them to each individual element, since IE6 doesn't support element prototypes.

这篇关于如何使document.querySelector在IE6中工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆