0

我脑子里有这个代码

<script language="JavaScript">
function alertSize() {
    // Get Browser Viewport Width
    var adjust = "<?php echo $adjust; ?>";
    // Get Browser Viewport Height
    var height = window.innerHeight ||
    document.documentElement.clientHeight ||
    document.body.clientHeight;
    // Get Browser Viewport Height
    var height = window.getSize().y;

}
window.onload = alertSize;


</script>

我的身体里有这个

<div id="bodyheight">

                </div>
                <script language="JavaScript">
                document.getElementById("bodyheight").style.height = height;</script>

但我进入正文代码:

'未捕获的 ReferenceError:高度未定义'

在头部代码中,我知道高度有一个值,当我输出“高度”作为弹出窗口时,会显示正确的值。

我知道'document.getElementById("bodyheight").style.height = ;' 当我放置一个固定值代替“高度”变量时工作。

但由于某种原因,我无法让“高度”变量在document.getElementById("bodyheight").style.height = height;.

4

2 回答 2

1

height is a function-local variable, and can only be used from within alertSize(). Your inline script is attempting to use it, but this variable is not in scope at that point.

Additionally, note that you use the var statement twice to create the height variable. You are redeclaring a local variable, and this is considered by many developers to be an error.

于 2013-03-21T17:08:22.760 回答
0

变量范围尝试

var height;
function alertSize() {
    // Get Browser Viewport Width
    var adjust = "<?php echo $adjust; ?>";
    // Get Browser Viewport Height
    height = window.innerHeight ||
    document.documentElement.clientHeight ||
    document.body.clientHeight;
    // Get Browser Viewport Height
    height = window.getSize().y;

}

更多信息:JavaScript 中变量的范围是什么?

希望这可以帮助

于 2013-03-21T17:10:28.777 回答