javascript 自定义 id class 选择器实现 支持链式操作
(function() { return window.Query = window.$ = function() { var _this = {}, selector = arguments[0]; $.prototype = _this; /** * 判断是对象是数组还是名值对象 * 返回0表示是数组,返回1表示是名值对象 */ function objOrArr(obj) { if (!obj) return; if (typeof obj === "object") { for (var attr in obj) { return 1; } return obj.push ? 0 : 1; } } /** * 确保所有对象都能call */ function applyAll(obj, callback) { if (!obj) return; var idx = 0 if (objOrArr(obj)) { for (var i in obj) { if (callback.call(obj[i], i, obj[i]) === false) { break; } } } else { for (var o = obj[0]; idx < obj.length&& callback.call(o, idx, o) !== false; o = obj[++idx]) { } } } /** * id选择器 */ function identity(selector) { if (selector) { return document.getElementById(selector); } } /** * 类选择器 */ function clazz(selector) { var result = []; if (selector) { var a = selector.split("."); var prefix = a[0] || "*"; var suffix = a[1]; applyAll(document.getElementsByTagName(prefix), function() { if (this.nodeType === 1 && this.id) { var classNames = this.className.split(/\s+/g); var finded = this; applyAll(classNames, function() { if (this == suffix) { result.push(finded); } }); } else { } }); } return result; } if (selector) { if (typeof selector === "string") { if (selector.indexOf(".") != -1) return clazz(selector); if (selector.indexOf("#") != -1){ return identity(selector.substring(selector.indexOf("#")+1)); }else{ return document.getElementsByTagName(selector); } } if (typeof selector === "object") { _this[0] = selector; return _this[0]; } if (typeof selector === "function") { return selector(_this); // selector 就是传入的function } } else { _this.toString(); } _this.toString = function() { return "[Query query]"; } return _this; } })(); /** * 简易弹出框 */ $.pop = function(url, title, options) { if(options){ window.open(url,title,options) }else{ window.open(url,title,'height=350,width=800,top=200,left=300,toolbar=no,menubar=no,scrollbars=no,resizable=no,location=no,status=no') } }, /** * 简易ajax请求 */ $.ajax = function(options) { var httpRequest = window.ActiveXObject? new ActiveXObject("Microsoft.XMLHTTP"): new XMLHttpRequest();httpRequest.onreadystatechange = function() { var dataType = options.dataType.toLowerCase(); httpRequest.readyState === 4&& httpRequest.status === 200&& options.callback(dataType === "json" ? eval("(" + httpRequest.responseText + ")") : dataType === "xml"? httpRequest.responseXML: httpRequest.responseText, options.context); }; httpRequest.open(options.mode, options.url, options.sync); if(options.mode.toLowerCase() === "post"){ httpRequest.setRequestHeader("CONTENT-TYPE", "application/x-www-form-urlencoded"); } options.mode.toLowerCase() === "get" ? httpRequest.send(null):httpRequest.send(options.params); }, /** * 去除所有空格 */ $.trim = function(str) { return str.replace(/(^\s*)|(\s*$)/g,""); }, /** * 去除左边空格 */ $.ltrim = function(str) { return str.replace(/(^\s*)/g, ""); }, /** * 去除右边空格 */ $.rtrim = function(str) { return str.replace(/(\s*$)/g, ""); }
相关推荐
nmgxzm00 2020-11-10
ifconfig 2020-10-14
hhanbj 2020-11-17
zfszhangyuan 2020-11-16
古叶峰 2020-11-16
一个智障 2020-11-15
jipengx 2020-11-12
81427005 2020-11-11
xixixi 2020-11-11
游走的豚鼠君 2020-11-10
苗疆三刀的随手记 2020-11-10
Web卓不凡 2020-11-03
小飞侠V 2020-11-02
帕尼尼 2020-10-30
爱读书的旅行者 2020-10-26
帕尼尼 2020-10-23
杏仁技术站 2020-10-23
淼寒儿 2020-10-22