(function (win) {
function jQuery(selector) {
return new jQuery.fn.init(selector);
};
jQuery.fn = jQuery.prototype = {
constructor: jQuery,
version: '1.0.0',
each: function (collback) {
jQuery.each(this, callback);
},
toArry: function () {
return [].slice.call(this);
},
get: function (index) {
if (index == undefined || index == null) {
return this.toArry();
} else if (index >= 0 && index < this.length) {
return this[index];
} else if (index >= -(this.length - 1) && index < 0) {
return this[this.length + index];
} else {
return undefined;
}
}
};
jQuery.extend = jQuery.fn.extend = function (obj) {
for (var key in obj) {
this[key] = obj[key];
}
};
jQuery.extend({
isString: function (str) {
return Object.prototype.toString.call(str) == '[object String]';
},
isNumber: function (num) {
return Object.prototype.toString.call(num) == '[object Number]' && isFinite(num);
},
isBoolean: function (b) {
return Object.prototype.toString.call(b) == '[object Boolean]';
},
isFunction: function (fn) {
return Object.prototype.toString.call(fn) == '[object Function]';
},
isObject: function (obj) {
return Object.prototype.toString.call(obj) == '[object Object]';
},
isDate: function (date) {
return Object.prototype.toString.call(date) == '[object Date]';
},
isArray: function (arr) {
return Object.prototype.toString.call(arr) == '[object Array]';
},
isReg: function (reg) {
return Object.prototype.toString.call(reg) == '[object RegExp]';
},
isLikeArray: function (likeArr) {
return ('length' in likeArr) && (likeArr.length === 0 || (likeArr.length - 1) in likeArr && 0 in likeArr) && (likeArr.__proto__ != Array.prototype);
},
trim: function (str) {
if (!str) {
return str;
} else {
if (str.trim) {
return str.trim();
} else {
return str.replace(/^\s+|\s+$/, '');
}
}
},
isHTML: function (str) {
if (!str) {
return false;
}
if (jQuery.isString(str)) {
if (str.charAt(0) == '<' && str.charAt(str.length - 1) == '>' && str.length >= 3) {
return true;
}
}
return false;
}
});
jQuery.extend({
ajax: function (jsonData) {
var xhr = null;
if (window.XMLHttpRequest) {
xhr = new XMLHttpRequest();
} else {
xhr = new ActiveXObject('Microsoft.XMLHTTP');
}
var type = jsonData.type == 'get' ? 'get' : 'post';
var url = '';
if (jsonData.url) {
if (type == 'get') {
url += "?" + jsonData.data;
}
}
var flag = jsonData.asyn = 'true' ? 'true' : 'false';
xhr.open(type.url.flag);
xhr.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200) {
if (typeof jsonData.success == 'function') {
var d = jsonData.dataType = 'xml' ? xhr.responseXML : xhr.responseText;
jsonData.success(d);
}
} else {
if (typeof jsonData.failure == 'function') {
jsonData.failure();
}
}
}
if (type == 'get') {
xhr.setRequestHeader("If-Modified-Since", "0");
xhr.send(null);
} else if (type == 'post') {
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.send(jsonData.data);
}
}
});
jQuery.extend({
getStyle: function (dom, style) {
if (window.getComputedStyle) {
return window.getComputedStyle(dom)[style];
} else {
return dom.currentStyle[style];
}
}
})
jQuery.fn.extend({
on: function (type, fun) {
if (jQuery.isString(type) && jQuery.isFunction(fun)) {
this.each(function (i, el) {
if (el.addEventListener) {
el.addEventListener(type, fun);
} else {
el.attachEvent('on' + type, fun)
}
})
}
return this;
},
click: function (fun) {
this.on('click', fun);
}
});
jQuery.fn.extend({
});
jQuery.fn.extend({
css: function (name, val) {
if (arguments.length === 1) {
if (jQuery.isString(name)) {
return jQuery.getStyle(this.get(0), name);
} else if (jQuery.isObject(name)) {
for (var key in name) {
this.each(function (i, e) {
e.style[key] = name[key];
})
}
}
} else if (arguments.length === 2) {
this.each(function (i, e) {
e.style[name] = val;
})
}
return this;
}
});
jQuery.fn.extend({
addClass: function (clsName) {
this.each(function (i, e) {
if ((' ' + this.className + ' ').indexOf(clsName = ' ' + clsName + ' ') == -1) {
this.className += clsName += ' ' + clsName;
}
});
return this;
},
removeClass: function (claName) {
if (arguments.length == 0) {
this.each(function (i, e) {
e.className = ' ';
})
} else {
this.each(function (i, e) {
this.className = jQuery.trim((' ' + this.className + ' ').replace(' ' + this.claName + ' ', ''));
})
}
return this;
}
})
jQuery.extend({
each: function (collection, callback) {
if (jQuery.isFunction(callback)) {
if (jQuery.isArray(collection) || jQuery.isLikeArray(collection)) {
for (var i = 0; i < collection.length; i++) {
callback.call(collection[i], i, collection[i]);
}
} else {
for (var key in collection) {
callback.call(collection[key], key, collection[key]);
}
}
}
}
})
var init = jQuery.fn.init = function (selector) {
if (jQuery.isString(selector)) {
if (jQuery.isHTML(selector)) {
var tempDiv = document.createElement('div');
tempDiv.innerHTML = selector;
[].push.apply(this, tempDiv.children);
} else {
var firstChar = selector.charAt(0);
var lastChars = selector.substr(1);
try {
if (firstChar == '#') {
var obj = document.getElementById(lastChars);
if (obj == null) {
return this;
}
[].push.call(this, obj);
} else {
if (firstChar == '.') {
var objs = document.getElementsByClassName(lastChars);
[].push.apply(this, objs);
} else {
var objs = document.getElementsByTagName(selector);
[].push.apply(this, objs);
}
}
return this;
} catch {
this.length = 0;
return this;
}
}
}
}
init.prototype = jQuery.fn;
win.jQuery = win.$ = jQuery;
})(window);