100行代码以内实现一个你可以工作中扩展的jquery

发布时间:2019-05-28 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了100行代码以内实现一个你可以工作中扩展的jquery脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

不喜欢逼逼,拿去直接扩展用完事儿。

;(function(window,factory){
    factory(window);
})(window,function(window){
    var jQuery=(function(){
        var jQuery=function(selector){
            return new jQuery.fn.init(selector);
        };
        jQuery.fn=jQuery.prototype={};
        var init=jQuery.fn.init=function(selector){
            if(!selector){
                return this;
            }
            if(typeof selector === "string"){
                if(selector.indexOf('<')!=-1){
                    this.ele = selector;                        
                }else{
                    //选择器
                    this.eles = document.querySelectorAll(selector);
                    var len = this.eles.length;
                    for(var i = 0;i<len;i++){
                        [].push.call(this,this.eles[i]);
                    }
                }                    
            }else if(typeof selector === "function" ){
                this.ready(selector);
            }else if(typeof selector === "object"){
                    return this;
            }
            return this;
        };
        init.prototype=jQuery.fn;
        jQuery.extend=jQuery.fn.extend=function(){
            var options, name, src, copy, copyIsArray, clone,
                    target = arguments[0] || {},
                    i = 1,
                    length = arguments.length,
                    deep = false;
                if (typeof target === "boolean") {
                    deep = target;
                    target = arguments[1] || {};
                    i = 2;
                }
                if (typeof target !== "object" && !jQuery.isFunction(target)) {
                    target = {};
                }
                if (i === length) {
                    target = this;
                    i--;
                }
                for ( ; i < length; i++) {
                    if ((options = arguments[ i ]) != null){
                        for(name in options){
                            src=target[name];
                            copy=options[name];
                            if(target===copy){
                                continue;
                            }
                            if(deep&&copy&&(jQuery.isPlainObject(copy)||(copyIsArray=jQuery.isArray(copy)))){
                                if(copyIsArray){
                                    copyIsArray=false;
                                    clone=src&&jQuery.isArray(src)?src:{};
                                }else{
                                    clone=src&&jQuery.extend(deep,clone,copy);
                                }
                                target[name]=jQuery.extend(deep,clone,copy);
                            }else if(copy!==undefined){
                                target[name]=copy;
                            }
                        }
                    }
                }
                return target;
        };
        jQuery.fn.extend({
            css:function(name,value){
                for(var i = 0;i<this.length;i++){
                    this[i].style[name] = value;
                }
                return this;
            },
            appendTo:function(context){
                    var parentNode = document.querySelector(context).parentNode;
                    parentNode.insertAdjacentHTML('afterBegin',this.ele);
            },
            ready:function(fn){
                document.addEventListener('DOMContentLoaded',fn,false);
            }
        });
        return jQuery;
    })();
    window.jQuery=window.$=jQuery;
    return jQuery;
});

回头吧class操作,事件绑定和ajax加上就基本够用了。

脚本宝典总结

以上是脚本宝典为你收集整理的100行代码以内实现一个你可以工作中扩展的jquery全部内容,希望文章能够帮你解决100行代码以内实现一个你可以工作中扩展的jquery所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。