jquery - javascript for loop with decimals - Stack Overflow

I'm trying to use this for loop in order to show divs. But I get a strange error from the jQuery l

I'm trying to use this for loop in order to show divs. But I get a strange error from the jQuery lib.

Error: Syntax error, unrecognized expression: =10]

I have read about the problems with javascript decimals, but I still can't understand why this won't work:

for (var i = 10.00; i >= ui.value; i -= 0.25) {
    $("data_id=" + Math.floor(i) + "]").show();
}

When hiding the divs, I use this and it works fine:

for (var i = 0.00; i < ui.value; i += 0.25) {
    $("[data_id=" + Math.floor(i) + "]").hide();
}

I'm trying to use this for loop in order to show divs. But I get a strange error from the jQuery lib.

Error: Syntax error, unrecognized expression: =10]

I have read about the problems with javascript decimals, but I still can't understand why this won't work:

for (var i = 10.00; i >= ui.value; i -= 0.25) {
    $("data_id=" + Math.floor(i) + "]").show();
}

When hiding the divs, I use this and it works fine:

for (var i = 0.00; i < ui.value; i += 0.25) {
    $("[data_id=" + Math.floor(i) + "]").hide();
}
Share Improve this question edited Sep 16, 2012 at 10:17 nbrooks 18.2k5 gold badges56 silver badges67 bronze badges asked Sep 16, 2012 at 10:10 8bitcat8bitcat 2,2445 gold badges30 silver badges59 bronze badges 1
  • 2 Why are you using fractional numbers, if you .floor() them anyways? Try to use an integer loop. – Gregor Commented Sep 16, 2012 at 10:13
Add a ment  | 

4 Answers 4

Reset to default 7

You forgot the [ in the first loop, this will work:

for (var i = 10.00; i >= ui.value; i -= 0.25) {
    $("[data_id=" + Math.floor(i) + "]").show();
}

You should transform this into an integer loop, if you are .floor()-ing the numbers, anyway.

You're missing your opening square bracket for the attribute equals selector:

for (var i = 10.00; i >= ui.value; i -= 0.25) {
    $("[data_id=" + Math.floor(i) + "]").show();
}


As others have mentioned, however, there is absolutely no reason to be using floats for this, since the call to .floor() essentially means you are calling .show() on each of the divs 4 times unnecessarily:

for (var i = 10; i >= ui.value; i--) {
    $("[data_id=" + i + "]").show();
}

This should acplish exactly you want, in about a quarter of the work.

You are missing a [ in your selector here:

$("data_id=" + Math.floor(i) + "]").show();

Which should be:

$("[data_id=" + Math.floor(i) + "]").show();

You should probably add ' around the value of data_id as well, so the final result should be:

$("[data_id='" + Math.floor(i) + "']").show();

You should never, ever rely on floating point arithmetic for iteration/indexing variables. They may run you into strange situations, and even worse, different processors handle floating points differently. Your example doesn't seem to have any side-effects of floating points, but using floating points is really a bad practice.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743722716a4496094.html

相关推荐

  • jquery - javascript for loop with decimals - Stack Overflow

    I'm trying to use this for loop in order to show divs. But I get a strange error from the jQuery l

    6天前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信