├── package.json ├── LICENSE ├── README.md └── index.js /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "mini-query", 3 | "version": "0.0.3", 4 | "description": "A mini element query selector.", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/barretlee/MiniQuery.git" 12 | }, 13 | "keywords": [ 14 | "query", 15 | "selector" 16 | ], 17 | "author": "Barret Lee ", 18 | "license": "MIT", 19 | "bugs": { 20 | "url": "https://github.com/barretlee/MiniQuery/issues" 21 | }, 22 | "homepage": "https://github.com/barretlee/MiniQuery#readme" 23 | } 24 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 小胡子哥 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Mini Query 2 | 3 | A mini element query selector. 4 | 5 | ```javascript 6 | function $(query) { 7 | var res = []; 8 | if (document.querySelectorAll) { 9 | res = document.querySelectorAll(query); 10 | } else { 11 | var firstStyleSheet = document.styleSheets[0] || document.createStyleSheet(); 12 | query = query.split(','); 13 | for(var i = 0, len = query.length; i < len; i++) { 14 | firstStyleSheet.addRule(query[i], 'Barret:Lee'); 15 | } 16 | for (var i = 0, len = document.all.length; i < len; i++) { 17 | var item = document.all[i]; 18 | item.currentStyle.Barret && res.push(item); 19 | } 20 | firstStyleSheet.removeRule(0); 21 | } 22 | if(res.item) { /* Fuck IE8 */ 23 | var ret = []; 24 | for(var i = 0, len = res.length; i < len; i++){ 25 | ret.push(res.item(i)); 26 | } 27 | res = ret; 28 | } 29 | return res; 30 | }; 31 | ``` 32 | 33 | ## Install 34 | 35 | - `npm install mini-query` 36 | - `git clone https://github.com/barretlee/MiniQuery` 37 | 38 | ## Usage 39 | 40 | ```html 41 | 42 | 45 | ``` 46 | 47 | ## LICENSE 48 | 49 | MIT. -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @author Barret Lee(http://barretlee.com/about/) 3 | * @description 寥寥几行代码,实现一个简单的元素选择器,兼容低版本 IE。 4 | */ 5 | void function(win, undefined){ 6 | 7 | umd("$", $); 8 | 9 | // mini Query 10 | function $(query) { 11 | var res = []; 12 | if (document.querySelectorAll) { 13 | res = document.querySelectorAll(query); 14 | } else { 15 | var firstStyleSheet = document.styleSheets[0] || document.createStyleSheet(); 16 | query = query.split(','); 17 | for(var i = 0, len = query.length; i < len; i++) { 18 | firstStyleSheet.addRule(query[i], 'Barret:Lee'); 19 | } 20 | for (var i = 0, len = document.all.length; i < len; i++) { 21 | var item = document.all[i]; 22 | item.currentStyle.Barret && res.push(item); 23 | } 24 | firstStyleSheet.removeRule(0); 25 | } 26 | if(res.item) { /* Fuck IE8 */ 27 | var ret = []; 28 | for(var i = 0, len = res.length; i < len; i++){ 29 | ret.push(res.item(i)); 30 | } 31 | res = ret; 32 | } 33 | return res; 34 | }; 35 | 36 | // UMD 37 | function umd(name, component) { 38 | switch (true) { 39 | case typeof module === 'object' && !!module.exports: 40 | module.exports = component; 41 | break; 42 | case typeof define === 'function' && !!define.amd: 43 | define(name, function() { 44 | return component; 45 | }); 46 | break; 47 | default: 48 | try { 49 | if (typeof execScript === 'object') { 50 | execScript('var ' + name); 51 | } 52 | } catch (error) {} 53 | window[name] = component; 54 | } 55 | }; 56 | 57 | }(window, void 0); --------------------------------------------------------------------------------