querySelector和querySelectorAll在IE6和IE7下的兼容性问题

Posted 南瓜壳

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了querySelector和querySelectorAll在IE6和IE7下的兼容性问题相关的知识,希望对你有一定的参考价值。

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;
    };
}

// 用于在IE6和IE7浏览器中,支持Element.querySelectorAll方法
var qsaWorker = (function () {
    var idAllocator = 10000;

    function qsaWorkerShim(element, selector) {
        var needsID = element.id === "";
        if (needsID) {
            ++idAllocator;
            element.id = "__qsa" + idAllocator;
        }
        try {
            return document.querySelectorAll("#" + element.id + " " + selector);
        }
        finally {
            if (needsID) {
                element.id = "";
            }
        }
    }

    function qsaWorkerWrap(element, selector) {
        return element.querySelectorAll(selector);
    }

    // Return the one this browser wants to use
    return document.createElement(‘div‘).querySelectorAll ? qsaWorkerWrap : qsaWorkerShim;
})();

以上是关于querySelector和querySelectorAll在IE6和IE7下的兼容性问题的主要内容,如果未能解决你的问题,请参考以下文章

getElementById和querySelector区别

重要选择器querySelector和querySelectorAll

document.querySelector和querySelectorAll方法

JavaScript中 querySelector 与 getElementById 方法的区别

JS选择器querySelector和~All,三个原生选择器

querySelector 与 getElementById [关闭]