• 写一个mini的JQuery



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

    })();




  • 相关阅读:
    inotify-java linux系统监听文件发生变化,实时通知java程序
    设置模式之单例模式(附上一个Objective-C编写的播放音乐的单例类)
    设计模式之观察者模式(关于OC中的KVOKVCNSNotification)
    设计模式之原型模式(深入理解OC中的NSCopying协议以及浅拷贝、深拷贝)
    设计模式之模板方法模式&&迪米特法则(代码Objective-C展示)
    iOS开发:深入理解GCD 第一篇
    设计模式之工厂方法模式(代码用Objective-C展示)
    iOS开发:一个高仿美团的团购ipad客户端的设计和实现(功能:根据拼音进行检索并展示数据,离线缓存团购数据,浏览记录与收藏记录的批量删除等)
    Xcode一些好用的插件,以及这些插件的管理器
    综合出现NSScanner: nil string argument libc++abi.dylib: terminat错误的解决方案
  • 原文地址:https://www.cnblogs.com/luowenjun-kio/p/7900018.html
Copyright © 2020-2023  润新知