简体   繁体   English

如何在jQuery中使用变量?

[英]How to use a variable in jQuery?

How to use variable in jQuery? 如何在jQuery中使用变量? I used the var i, Here's the code: 我使用了var i,这是代码:

var i=0;
for (i=0;i<=5;i++){
   $('.slide:eq(i)').delay(3000).fadeOut(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 如果要使用:eq()而不是.eq(i),则需要执行

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. 您只需要将i的值放入jQuery选择器即可。

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

Why set i to 0 twice? 为什么要两次将我设置为0? 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. 您现在正在使用变量i作为字符串。 SO jquery will see i really as as i. 因此,jQuery会像我一样看待我。 Try concatenating it like above. 尝试像上面那样串联它。

try this : 尝试这个 :

var i=0;

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

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

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM