1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
| //遍历器 var each = function (arr, callback) { for (var i = 0; i < arr.length; i++) { if (callback.call(arr[i], i, arr[i]) === false) { break; } } }; //去首尾空格 var myTrim = function (str) { if (String.prototype.trim) { return str.trim(); } else { return str.replace(/^\s+|\s+$/g, '') } } /*基础查询方法*/ var _get = function (selector, context, list) { list = list || []; context = context || document; var reg = /^(?:#([\w-]+)|\.([\w-]+)|(\w+)|(\*))$/, m = reg.exec(selector); if (m) { if (context.nodeType) { context = [context]; } if (typeof context === 'string') { context = _get(context); } each(context, function () { if (m[1]) { list = _getId(m[1], this, list); } else if (m[2]) { list = _getClassName(m[2], this, list); } else if (m[3]) { list = _getTag(m[3], this, list); } else if (m[4]) { list = _getTag("*", this, list); } }); } return list; }; var _getId = function (Id, context, list) { list = list || []; list.push(document.getElementById(Id)); return list; }; var _getClassName = function (className, context, list) { list = list || []; if (document.getElementsByClassName) { list.push.apply(list, context.getElementsByClassName(className)); } else { var tags = context.getElementsByTagName('*'); for (var i = 0; i < tags.length; i++) { if ((' ' + tags[i] + ' ').indexOf(' ' + className + ' ') != -1) { list.push(tags[i]); } } } return list; }; var _getTag = function (Tag, context, list) { list = list || []; list.push.apply(list, context.getElementsByTagName(Tag)); return list; }; /**************** 选择器的操作*********************/ var $ = function (selectors, context, list) { list = list || []; var selectNew = selectors.split(','); each(selectNew, function () { var tmp = this.split(' '); var c = context; for (var i = 0; i < tmp.length; i++) { if (!tmp[i])continue; c = _get(tmp[i], c); } list.push.apply(list, c); }); return list; };
|