简体   繁体   中英

jQuery error, variable is not defined

I'm trying following code to create a plugin. I'm getting error at the line if(options.controls == true)

The error I get is ' options is not defined '.

How should I define it?

(function($) {

    $.fn.jwslider = function(options){
        var defaults = {            
            controls: true           
        };      
        var options = $.extend(defaults, options);      
    }

    init();

    function init()
    {
        if(options.controls == true)
        {
            alert("controls true");
        }       

    } 

}(jQuery)); 

You have to define the options variable outside the scope of function.

Currently it is defined in the scope of $.fn.jwslider hence it is giving the error.

(function($) {

    $.fn.jwslider = function(options){
        var defaults = {            
            controls: true           
        };      
        var options = $.extend(defaults, options);      

        init();

        function init()
        {
            if(options.controls == true)
            {
                alert("controls true");
            }       
        } 
    }
}(jQuery)); 

Then the options is accessible inside init function

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM