(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, "");
}