jquery alert($(" window")。height()+"" + $(" document")。height()+"&# 34; + $(" window")。width());将所有值返回null

时间:2014-10-15 20:28:56

标签: javascript jquery html

我正在尝试获取浏览器窗口的高度和宽度,因此它返回null,示例代码如下所示 我还使用了$ stack(“window”)。我在stackoverflow中找到的load(function())仍然返回null。

<html>
  <head></head>
  <body>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></scipt>
    <script>
      $(document).ready(function(){
        //$("#page").outerWidth($("window").width(),true);
        // $("#page").outerHeight($("window").height(),true);
        alert($("window").height() + " " + $("document").height() + " " +$("window").width());
      });
    </script>
  </body>
</html>

2 个答案:

答案 0 :(得分:4)

$("window")应为$(window)

当您使用$("window")时,您要求jQuery使用tagName&#34; window&#34;选择所有元素。 (并且没有)。当您使用$(window)时,您实际上是在告诉jQuery包装window对象。

而且,为了清楚起见,这也适用于$("document")$(document)的变化。

答案 1 :(得分:0)

就是这样:

$(document).ready(
                   function () {

                       //$("#page").outerWidth($("window").width(),true);
                       // $("#page").outerHeight($("window").height(),true);
                       alert($(window).height() + " " + $(document).height() + " " + $(window).width());

                   }

                      );