2010-10-12 66 views
1

回调之后,我无法获得& jQuery的循环插件!jQuery Cycle Plugin回调错误

我不确定发生了什么问题,我甚至尝试使用文档中的示例代码。

下面的代码:

$(document).ready(function(){ 

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

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

和它抛出一个错误: “错误:opts.before [0]。适用不是一个函数”

和在铬:“未捕获类型错误:对象onBefore has no method'apply'“

发生了什么!?

回答

2

错误是因为.apply()是一个函数的方法,而不是字符串......而'onBefore'是一个字符串。取而代之的是,不要使用字符串...使用直接引用,就像这样:

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

或匿名函数:

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

哦,新手的错误。不能相信我忽略了这一点。谢谢!! – neil 2010-10-18 02:44:06