当前位置:首页 > querySelector querySelectorAll 兼容处理,让ie支持querySelector 和 querySelectorAll

querySelector querySelectorAll 兼容处理,让ie支持querySelector 和 querySelectorAll

发布于 2018-06-04 阅读 651 次 浏览器兼容
在简书上看到的这段兼容代码,看不太懂,先收藏起来了,欢迎大牛抛转解释 ```javascript 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; })(); ``` 出处链接:https://www.jianshu.com/p/872d69047dda
相关文章