码迷,mamicode.com
首页 > 其他好文 > 详细

封装自己的yQuery

时间:2014-07-13 21:09:28      阅读:140      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   for   cti   io   

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);
}

 

封装自己的yQuery,布布扣,bubuko.com

封装自己的yQuery

标签:style   blog   color   for   cti   io   

原文地址:http://www.cnblogs.com/yzenet/p/3840921.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!