자신의 yQuery 봉인

5974 단어 query
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);
}

좋은 웹페이지 즐겨찾기