首页 > 代码库 > 封装自己的yQuery

封装自己的yQuery

function myAddEvent(obj, sEv, fn) {    if (obj.attachEvent) {        obj.attachEvent(on + sEv, fn)    } else {        obj.addEventListener(sEv, fn, false);    }}function getByClass(oParent, sClass) {    var aEle = oParent.getElementByTagName(*);    var aResult = [];    var i = 0;        for (var i = 0; i <aResult.length; i++) {        aResult.push(aEle[i]);    }    return aResult;}function VQuery(vArg) {    //用来保存选中的元素    this.elements = [];    switch (typeof vArg) {        case function:            //window.onload = vArg;            myAddEvent(window, load, vArg);            break;        case string:            switch (vArg.charAt(0)) {                case #:   //ID                    var obj = document.getElementById(vArg.substring(1));                    this.elements.push(obj);                    break;                case .:   //class                    getByClass(document, vArg.substring(1));                    break;                default:    //tagName                    this.elements = document.getElementsByTagName(vArg);            }                            break;        case object:            this.elements.push(vArg);            break;    }}VQuery.prototype.click = function (fn) {    var i = 0;    for (var i = 0; i < this.elements.length; i++) {        myAddEvent(this.elements[i], click, fn);    }}function $(vArg) {    return new VQuery(vArg);}