[英]jquery plugin, boolean callback parameter
我試圖從jQuery插件內部將布爾參數傳遞給回調,但參數始終未定義。
每次單擊鏈接時,onSwitch回調參數應在true和false之間交替。 使用調試器,我可以看到傳遞給回調調用函數的值已正確定義為true或false,但是在回調實現中,它變為undefined。
我試圖尋找像其他一些類似的問題, 這 本與此 ,但似乎無法得到這個工作。
這是我的插件定義:
(function ($) {
$.fn.switcherButton = function (options) {
// Set the default options
var settings = $.extend({},$.fn.switcherButton.defaults, options);
this.click(function () {
$.fn.switcherButton.switched = !$.fn.switcherButton.switched;
settings.onSwitch.call($.fn.switcherButton.switched);
});
return this;
};
// Plugin defaults – added as a property on our plugin function.
$.fn.switcherButton.defaults = {
onSwitch: function() {}
};
$.fn.switcherButton.switched = false;
}(jQuery));
HTML:
<a id="switchTest" href="#">switch</a>
插件初始化:
$("#switchTest").switcherButton({
onSwitch: function(switched){
if(typeof switched === "undefined")
alert("callback param = undefined");
else
if(switched)
alert("callback param = true");
else
alert("callback param = false");
}
});
我在這里創建了一個問題的jsfiddle。
將settings.onSwitch.call($.fn.switcherButton.switched)
更改為settings.onSwitch($.fn.switcherButton.switched)
調用用於設置此參數,而不是函數的實際參數
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.