[英]Jquery JavaScript variable is not defined, inexperience caused error
[英]jQuery error, variable is not defined
我正在嘗試按照以下代碼創建插件。 我在if(options.controls == true)
行上遇到錯誤
我得到的錯誤是“ options is not defined
”。
我應該如何定義?
(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));
您必須在功能范圍之外定義options
變量。
當前它在$.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));
然后可以在init
函數中訪問這些options
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.