码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2018-04-10 15:06:56      阅读:182      评论:0      收藏:0      [点我收藏+]

标签:null   end   scroll   move   attribute   RKE   sid   pen   方法   

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下的兼容性问题

标签:null   end   scroll   move   attribute   RKE   sid   pen   方法   

原文地址:https://www.cnblogs.com/wangshengli520/p/8778369.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!