变量计算不起作用(jquery/javascript)

Variable calculation not working (jquery/javascript)

本文关键字:javascript jquery 计算 不起作用 变量      更新时间:2023-09-26

好吧,也许我只是瞎了眼,错过了一些明显的东西,但这是我见过的最奇怪的问题。。。

我有以下代码:

var innerHeight;
$(function(){
    var contentTop = 116;  // calculated on the fly from page header
    var contentBottom = 37;  // calculated on the fly from page footer
    console.log('window.height'); 
    console.log($(window).height());
    console.log('contentTop'); 
    console.log(contentTop);
    console.log('contentBottom'); 
    console.log(contentBottom);
    console.log('$( window ).height() - contentTop - contentBottom');
    console.log((parseInt($( window ).height()) - parseInt(contentTop) - parseInt(contentBottom)));
    innerHeight = (parseInt($( window ).height()) - parseInt(contentTop) - parseInt(contentBottom));
    console.log('innerHeight'); 
    console.log(innerHeight);
});

它应该吐出来:

window.height
936
contentTop
116
contentBottom
37
$( window ).height() - contentTop - contentBottom
783
innerHeight
783

它确实吐出来了:

window.height
936
contentTop
116
contentBottom
37
$( window ).height() - contentTop - contentBottom
783
innerHeight
936

注意最后一个数字。

我尝试了各种各样的变化,使innerHeight成为公式$( window ).height() - contentTop - contentBottom的结果,但它只是失败了。。。

我运行的是Firefox Nightly(64位)版本28.0a1。

在Chrome中,我只是注意到结果可能在8点左右,但还没有时间深入查看,因为Firefox的问题让我大吃一惊。。。

有线索吗?

仅供参考,我已经尝试在有问题的行的三个部分中的每一个周围添加parseInt()

尝试使用这个:

Math.floor($( window ).height() - contentTop - contentBottom);
睡眠不足真的对你不好。。。

显然,innerHeight不能用作变量——如果我只是写它,它意味着window.innerHeight。。。

更改了变量名,现在可以工作了。

现在我真的希望我能收回这个问题。。。