How to make document.querySelector work in IE6

前端 未结 2 1338
抹茶落季
抹茶落季 2021-01-14 17:45

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

document.querySelector(selector)

onl

相关标签:
2条回答
  • 2021-01-14 18:24

    You could use a polyfill, like this one, however still using IE6, is the IT analogue of necromancy.

    The mentioned polyfill is based on polyfill.js, which can be found here, this provides polyfills for a lot of ECMA 5 functions.

    I will post the current state of the script, maybe it will useful in the future (though I really hope, it won't be :) ):

    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;
        };
    }
    
    0 讨论(0)
  • 2021-01-14 18:50

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


    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;
        }
    @*/
    

    Although I would never countenance using for-in like that; details and alternatives in this other answer.

    And by inference, 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;
        }
    @*/
    

    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.

    0 讨论(0)
提交回复
热议问题