开发者

How to use a variable in jQuery?

How to use variable in jQuery? I used the var i, Here's the code:

var i=0;
for (i=0;i<=5;i++){
   $('.slide:eq(i)').delay(3000).fad开发者_如何学编程eOut(500);
}

Thank you.


It doesn't "work" because it's treated like a plain string.

You need to concatenate with '+'.

$('.slide:eq('+i+')').delay(3000).fadeOut(500);

You can also use:

$('.slide').eq(i).delay(3000).fadeOut(500);

which is clearer.


Try this instead...

for (i=0;i<=5;i++){
   $('.slide').eq(i).delay(3000).fadeOut(500);
}

The reason is that when you wrap the string in quotes, it's just a string!

If you want to use :eq() instead of .eq(i) you would need to do

for (i=0;i<=5;i++){
   $('.slide:eq('+i+')').delay(3000).fadeOut(500);
}


You just need to place the value of i into the jQuery selector.

var i=0;
for (i=0;i<=5;i++){
   $('.slide:eq(' + i + ')').delay(3000).fadeOut(500);
}


Why set i to 0 twice? Try something like below;

for (var i=0;i<=5;i++){
   $('.slide:eq('+ i + ')').delay(3000).fadeOut(500);
}

What you are doing now is using the variable i as an string. SO jquery will see i really as as i. Try concatenating it like above.


try this :

var i=0;

for (i=0;i<=5;i++){

   $('.slide:eq(' + i + ')').delay(3000).fadeOut(500);

}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜