我正在编写一个jQuery插件,它有几个回调函数,并且需要允许其中一个回调函数运行。有问题的回调是'beforeListItemSelect',所以如果回调函数返回false,我自然希望回调后的插件代码不运行。下面是我的插件代码的一部分:
$listItems.live('click', function() {
$.isFunction(options.beforeListItemSelect) && options.beforeListItemSelect.call(this);
// perform select action (apply css, etc)
$.isFunction(options.afterListItemSelect) && options.afterListItemSelect.call(this);
});因此,如果我调用插件(myPlugin),并且我像这样定义了beforeListItemSelect回调:
$('#someDiv').myPlugin({
beforeListItemSelect: function() {
return false;
}
});我希望插件在beforeListItemSelect回调之后终止。如果回调返回false,有谁知道如何设置插件不继续吗?
非常感谢。
发布于 2010-07-15 23:42:24
更少的语法糖,事情又开始变得简单起来:
var result;
if ($.isFunction(options.beforeListItemSelect))
result = options.beforeListItemSelect.call(this);
}
if (result === false) return;当然,您仍然可以使用您喜欢的所有语法糖:
$.isFunction(options.beforeListItemSelect) && result = options.beforeListItemSelect.call(this);但这并不能确切地提高代码的易读性,也不能增强任何东西。只是..。有可能。然而,并不是所有可能的事情都应该做。
https://stackoverflow.com/questions/3257205
复制相似问题