分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 代码编程

写一个mini的JQuery

发布时间:2023-09-06 01:27责任编辑:郭大石关键词:暂无标签


(function  () {

    var _$ = window.$;
    var _jQuery = window.jQuery;
    
    var jQuery = window.jQuery = window.$ = function(selector){

        return new jQuery.fn.init(selector);
    };


    jQuery.fn = jQuery.prototype = {
        init:function(selector){
            var elements = document.querySelectorAll(selector);
            Array.prototype.push.apply(this,elements);
            return this;    
        },
        jQuery:"1.0.0",
        length:0,
        size:function(){
            return this.length;
        }

    };
    jQuery.fn.init.prototype = jQuery.fn;

    jQuery.extend = jQuery.fn.extend = function(){
        var o = arguments[0];
        for(var p in o){
            this[p] = o[p];
        }
    };


    jQuery.extend({
        trim:function(text){
            return (text||"").replace(/^\s+|\s+$/g,"");
        },
        noConflict:function(){
            window.$ = _$;
            window.jQuery = _jQuery;
            return jQuery;
        }
    });


    jQuery.fn.extend({
        get:function(num){
            return this[num];
        },
        each:function(fn){
            for(var i = 0 ;i< this.length; i++){
                fn(i,this[i]);
            }
            return this;
        },
        css:function(){
            var l = arguments.length;
            if(l == 1){
                return this[0].style[arguments[0]];
            } else {
                var name = arguments[0];
                var value = arguments[1];
                this.each(function(index,ele) {
                    ele.style[name] = value;

                });
            }
            return this;
        },
        hide:function(){//隐藏元素
            this.each(function(index,ele){
                ele.style.display = "none";
            });
        },
        show:function(){//显示元素
            this.each(function(index,ele){
                ele.style.display = "block";
            });
        },
        addClass:function(){ //添加(class)类
            var name = arguments[0];
            this.each(function(index,ele){
                var ele_class = ele.className,
                blank = (ele_class != ‘‘) ? ‘ ‘ : ‘‘;
                added = ele_class + blank + name;
                ele.className = added;
            });
        },
        removeClass:function(){ //删除(class)类
            var name = arguments[0];
            this.each(function(index,ele){
                var obj_class = ‘ ‘+ele.className+‘ ‘;
                obj_class = obj_class.replace(/(\s+)/gi, ‘ ‘),
                removed = obj_class.replace(‘ ‘+name+‘ ‘, ‘ ‘);
                removed = removed.replace(/(^\s+)|(\s+$)/g, ‘‘);
                ele.className = removed;
            });
        },
        remove:function(){ //删除属性
            var name = arguments[0];
            this.each(function(index,ele){
                ele.attributes.removeNamedItem(name);
            });
        },
        width:function(){ //设置宽度
            var name = arguments[0];
            this.each(function(index,ele){
                ele.style.width = name;
            });
        },
        height:function(){ //设置高度
            var name = arguments[0];
            this.each(function(index,ele){
                ele.style.height = name;
            });
        },
        getWidth:function(){ //获取对象宽度
            this.each(function(index,ele){
                var gw = ele.offsetWidth;
                console.log(gw);
            });
        },
        getHeight:function(){ //获取对象高度
            this.each(function(index,ele){
                var gh = ele.offsetHeight;
                console.log(gh);
            });
        },
        on:function(eventName,callback){//on事件
            this.each(function(index,ele){
                ele[eventName] = callback ;
            });
        },
        first:function(){//获取该元素的第一个子元素
            this.each(function(index,ele){
                var ss = ele.children[0];
                console.log(ss);
            });
        },
        allEle:function(){//获取该元素的全部子元素
            this.each(function(index,ele){
                for(var i=0;i<ele.children.length;i++){
                    var ss = ele.children[i];
                    console.log(ss);    
                }
                
            });
        },
        
    });

})();




写一个mini的JQuery

原文地址:http://www.cnblogs.com/luowenjun-kio/p/7900018.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved