单纯自己看,没写注释。。。
function myAddEvent(obj, sEv, fn) { if (obj.attachEvent) { obj.attachEvent('on' + sEv, function () { fn.call(obj) }); } else { obj.addEventListener(sEv, fn, false) } } function getByClass(oparent, sclass) { var aEle = oparent.getElementsByTagName('*'); var aResult = []; var i = 0; for (i = 0, len = aEle.length; i < len; i++) { if (aEle[i].className == sclass) { aResult.push(aEle[i]) } } return aResult; } function vq(vArg) { this.elements = []; switch (typeof vArg) { case "function": myAddEvent(window, "load", vArg); break; case "string": switch (vArg.charAt(0)) { case "#": var obj = document.getElementById(vArg.substring(1)); this.elements.push(obj); break; case ".": this.elements = getByClass(document, vArg.substring(1)); break; default: this.elements = document.getElementsByTagName(vArg); } break; case "object": this.elements.push(vArg) } } vq.prototype.click = function (fn) { for (var i = 0; i < this.elements.length; i++) { myAddEvent(this.elements[i], "click", fn); } } function $(vArg) { return new vq(vArg) } vq.prototype.show = function () { for (var i = 0; i < this.elements.length; i++) { this.elements[i].style.display = "block";//咱不考虑行间对象 } } vq.prototype.hide = function () { for (var i = 0; i < this.elements.length; i++) { this.elements[i].style.display = "none" } } vq.prototype.hover = function (fnOver, fnOut) { for (var i = 0; i < this.elements.length; i++) { myAddEvent(this.elements[i], "mouseover", fnOver); myAddEvent(this.elements[i], "mouseout", fnOut) } } vq.prototype.css = function (attr, value) { if (arguments.length == 2) {//设置样式 for (var i = 0; i < this.elements.length; i++) { this.elements[i].style[attr] = value; } } else {//获取样式 return getStyle(this.elements[0], attr); } } function getStyle(obj, attr) { if (obj.currentStyle) { return obj.currentStyle[attr] } else { return getComputedStyle(obj, false)[attr] } } vq.prototype.toggle = function () { for (var i = 0; i < this.elements.length; i++) { addToggle(this.element s[i]) } function addToggle(obj) { var count = 0; myAddEvent(obj, "click", function () { arguments[count%arguments.length].call(obj); count++; }) } }