关闭

my_jquery

236人阅读 评论(0) 收藏 举报
/*
 * myjquery 0.1 - Javascript
 *
 * Copyright (c) 2011 zhuwei
 *
 * $Date: 2011-04-20$
 */
 var idExpr = /^#([\w-]+)$/;
 var singExpr = /^.\s+.$/;
 var toLower = Object.prototype.toLowerCase;
 var agent = navigator.userAgent.toLowerCase();
 var $ = myjquery = window.$ = window.myjquery = function(selector, content){
     return new myjquery.fn.init(selector, content);
 }
myjquery.fn = myjquery.prototype = {
    version: "myjquery 0.1",
    length: 0,
    init: function(selector, content){
        if (typeof selector == "string"){
            var match = idExpr.exec(selector);
            var sing = singExpr.exec(selector);
            if (match && match[1]){
                var emet = document.getElementById(match[1]);
                if (emet){
                    this[0] = emet;
                    this.length = 1;
                }
            }
            else if (sing && sing[1]){
                var emets = document.getElementsByTagName(sing[0]);
                var j = 0, k = 0, l = 0, len = emets.length;
                for (;j < len; j++){
                    for (;l < emets[j].childNodes.length;l++){
                        if (sing[1] === emets[j].childNodes[l].tagName.toLowerCase()){
                            this[k++] = emets[j].childNodes[l];
                            this.length = k;
                        }
                    }
                    
                }
            }
            else{
                var emets = document.getElementsByTagName("*");
                var j = 0, k = 0, len = emets.length;
                for (;j<len;j++){
                    if (selector === emets[j].type || selector === emets[j].tagName.toLowerCase()){
                        this[k++] = emets[j];
                        this.length = k;
                    }     
                }
            }
        }
        else{
            ;
        }

        this.content = document;
        this.selector = selector;
        return this;
    },
    css: function(name, val){
        if (val){
            this.each(function(){
                this.style[name] = val;
            });
        }
        else{
            if (typeof(name) == "string"){
                return this[0] ? this[0].style[name] : null;
            }
            else{
                this.each(function(){
                    for (var key in name){
                        this.style[key] = name[key];
                    }
                });
            }
        }
    },
 
text: function(val){
        if (val){
            this.each(function(){
                this.value = val;
            }, arguments);
        }
        else{
            return this[0] ? this[0].value : null;
        }
    },
    html: function(val){
        if (val){
            this.each(function(){
                this.innerHTML = val;
            });
        }
        else{
            return this[0] ? this[0].innerHTML : null;
        }
    },
    val: function(value){
        if (value){
            
        }
    },
    height: function(val){
        if (val){
            this.each(function(){
                this.style.height = val;
            }, arguments);
        }
        else{
            return this[0] ? this[0].style.height : null;
        }
    },
 
width: function(val){
       if (val) {
           this.each(function(){
               this.style.width = val;
           });
       }
       else{
           return this[0] ? this[0].style.width : null;
       }
    },
    attr: function(name, value){
        if (value){
            this.each(function(){
                this[name] = value;
            });
        }
        else{
            if (typeof(name) == "string"){
                return this[0] ? this[0].getAttribute(name) : null;
            }
            else{
                this.each(function(){
                    for (var key in name){
                        this.setAttribute(key, name[key]);
                    }
                });
            }
        }
    },
    removeAttr: function(name){
        this.each(function(){
            this.removeAttribute(name);
        });
    },
    addClass: function(className){
        if (className){
            this.each(function(){
                this.className += this.className ? "" : className;
            });
        }
    },    
    toggleClass: function(className){
        if (className){
            this.each(function(){
                if (this.className.indexOf(className) != -1){
                    var classArray = className.split(' ');
                    for (var t in classArray){
                        this.className = this.className.replace(className, "");
                    }
                }
                else
                    this.className += this.className ? "" : className;
            });
        }
    },
    removeClass: function(className){
        if (className){
            this.each(function(){
                var classArray = className.split(' ');
                for (var t in classArray){
                    this.className = this.className.replace(className, "");
                }
            });
        }
    },
    hasClass: function(className){
        return this.is("."+className);
    },
    each: function(callback, args){
        return myjquery.each(this, callback, args);
    }
}
 
myjquery.fn.init.prototype = myjquery.prototype;
myjquery.each = function (obj, callback, args){
    var i = 0; len = obj.length;
    if (args == undefined){
        for (var value = obj[i]; i<len && callback.call(value,i,value) !== false;){
            value = obj[++i];
        }
    }
    else{
        for (; i < len ;){
            if (callback.apply(obj[i++], args) === false){
                break;
            }
        }
    }
}
$("label").html(333);
myjquery.browser = {
    msie: /msie/.test(agent),
    firefox:/firefox/.test(agent),
    opera:/opera/.test(agent),
    safari:/safari/.test(agent) && !/chrome/.test(agent),
    chrome:/chrome/.test(agent)
}

 

 

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:325718次
    • 积分:8911
    • 等级:
    • 排名:第2229名
    • 原创:1096篇
    • 转载:14篇
    • 译文:0篇
    • 评论:5条
    最新评论