calculate a number without a variable in javascript
Given this code:
if(ipadmenuheight < contentheight).css('top', '' + contentheight + 44 + 'px');
Let's say contentheight=500
then this code snippet returns 50044px
. How can it be a sum instead and return 544px
?
Do I need开发者_StackOverflow社区 to use a variable or can I do this inline?
Use a parenthesis to sum both numbers. If not, they will be appended as strings:
...css('top', (contentheight + 44) + 'px');
By the way, the first empty string ''
is not needed, so you could also do:
...css('top', contentheight + 44 + 'px');
Use parentheses to force numerical addition:
('top', '' + (contentheight + 44) + 'px');
or just take off the leading string.
('top', contentheight + 44 + 'px');
Try
if(ipadmenuheight < contentheight).css('top', '' + (contentheight + 44) + 'px');
or eventually
if(ipadmenuheight < contentheight).css('top', '' + (parseInt(contentheight, 10) + 44) + 'px');
if contentheight is a string.
精彩评论