开发者

jQuery Cycle Plugin callback error

I can't get before & after callbacks to work with the cycle plugin for jQuery!

I'm not sure what's going wrong, i even tried using the sample code from the documentation.

Here's the code:

$(document).ready(function(){

    function onBefore() { alert('before'); }

    $('.slideshow').cycle({
        before: 'onBefore'
    });
});

and it throws an error: 开发者_如何学Go"Error: opts.before[0].apply is not a function"

and in chrome: "Uncaught TypeError: Object onBefore has no method 'apply'"

what is happening!?


The error is because .apply() is a method on functions, not on strings...and 'onBefore' is a string. Instead, don't use a string...use a direct reference, like this:

$(document).ready(function(){    
    function onBefore() { alert('before'); }    
    $('.slideshow').cycle({
        before: onBefore
    });
});

Or an anonymous function:

$(function(){
    $('.slideshow').cycle({
        before: function() { alert('before'); }
    });
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜