简体   繁体   English

jQuery错误,未定义变量

[英]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) 我在if(options.controls == true)行上遇到错误

The error I get is ' options is not defined '. 我得到的错误是“ 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. 您必须在功能范围之外定义options变量。

Currently it is defined in the scope of $.fn.jwslider hence it is giving the error. 当前它在$.fn.jwslider的范围内定义,因此它给出了错误。

(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 然后可以在init函数中访问这些options

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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