首页 > this在函数当中的运用,有不解,求指点

this在函数当中的运用,有不解,求指点

;
(function($){
    var defaults={
        priEle:0,  //初始选中何值
        getValue:true,//是否获取value
        getText:true,//是否获取选项内容
        getIndex:true,//是否获取索引
        };
    
    $.fn.proSelect=function(options){
        
        var opts=$.extend(defaults,options);
        var obj=$(this);
        
        //初始化
        obj.find('option').eq(opts.priEle).attr('selected',true);
        getVal();
        
        //改变状态
        obj.change(function(){
            getVal();
            });
        
        //获取value
        function getVal(){
            if(opts.getValue===true){
                var value=obj.find('option:selected').val();  //这里的obj是不是变了,和var obj=$(this);指向的对象不一样,要是让其一样,该怎么做啊?????????
                return value;
                }
            }
        
        return this;
        };
    
    
    })(jQuery);

js的function可以看成是一个对象,也可以理解为类,function中的this就是指这个对象 ,$(this)就是你构建的 proselect对象


为啥会变,全局变量啊。。。具体你代码出了什么问题obj就是调用proSelect函数的Jquery对象

【热门文章】
【热门文章】