IE8 subtracts wrong in JavaScript / jQuery?
In every browser, Win / Mac, Chrome, Safari, Firefox, Opera, IE6 and IE7, they ALL get the following console output:
352
254
But in IE8 I get:
414
434
454
474
Here is my JS / jQuery code:
$('#top-breadcrumbs').children('a').each(function(i){
if(!$(this).hasClass('permanent')){
if(permItemWidth+rmItemWidth > $('#top-breadcrumbs').width()){
$(this).addClass('removed');
rmItemWidth = rmItemWidth-$(this).width()+20;
}
}
});
The log code I have above writes the NEW value rmItemWidth
after it is reset in that 2ndif
source to share
I posted here and then finally realized I was playing around with the code. So change? Well no real code needed to change haha IE8, in fact I guess everything is correct ... weird:
Before:
$(this).addClass('removed');
rmItemWidth = rmItemWidth-$(this).width()+20;
After:
rmItemWidth = rmItemWidth-$(this).width()+20;
$(this).addClass('removed');
Because "this" is removed on the first line in the previous one, so technically it won't have any width. That's why it was only adding 20 each time ... IE8 really does it right and no one else ?!
source to share
Without looking at all the code, the expected result cannot be determined.
Instead, I suggest writing correct HTML, valid CSS, and figuring out exactly what to do to get the desired calculation.
The jQuery coding style is characterized by the following:
$ ('# top-breadcrumbs'). width ()
- inside the body of the function that is called in the loop.
The approach you took with jQuery is very inefficient.
source to share