传递self时jquery each()索引
我试图在此循环的每次迭代期间访问元素索引。
$('.slide').hide().repeat().each($).fadeIn($).wait(1000, function(){ //do stuff }).wait(noHover).fadeOut($);
我尝试过这样的事情:
$('.slide').hide().repeat().each(i, $).fadeIn($).wait(1000, function(){ alert(i); }).wait(noHover).fadeOut($);
显然,我不明白正确的方法。
插件扩展即时使用:
http://creativecouple.github.com/jquery-timing/examples/pause-cycle-on-hover.html
这是一个小提琴,可以更好地打破这个:
http://jsfiddle.net/zGd8a/
一个办法:
http://jsfiddle.net/zGd8a/8/
jsFiddle演示
$('.slide').hide().repeat().each($).fadeIn($).wait(1000, function(){ var idx = $(this).index(); // here you go! $('body').append(idx); //here i need access to index number of element }).wait(noHover).fadeOut($);
在这种情况下, each
函数都采用第二个参数(回调函数) – $
并且不执行任何操作 。 您应该将警报放在回调函数体中,该函数是each
函数的第二个参数,而不是wait
函数。 像这样:
$('.slide').hide().repeat().each(function(i, element) { alert(i); }).fadeIn($).wait(1000, function(){ //do stuff }).wait(noHover).fadeOut($);
更多信息请访问: http : //api.jquery.com/each/