最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Why is the width of the window null? - Stack Overflow

programmeradmin5浏览0评论

I am trying to alert the size of the window and the size of an element in order to put the element where I want it to be.

<script type="text/javascript"></script>
<script type="text/javascript">
  $(document).ready(function(){
    var element=$(".element").width();
    var window=$(window).width();
    alert("Element: "+element+" "+"Window: "+window);
  });
</script>

When I open it in the browser, I get the width of the element but the width of the window is null. What am I doing wrong?

I am trying to alert the size of the window and the size of an element in order to put the element where I want it to be.

<script type="text/javascript"></script>
<script type="text/javascript">
  $(document).ready(function(){
    var element=$(".element").width();
    var window=$(window).width();
    alert("Element: "+element+" "+"Window: "+window);
  });
</script>

When I open it in the browser, I get the width of the element but the width of the window is null. What am I doing wrong?

Share Improve this question edited Jul 5, 2012 at 0:55 balexandre 75.1k47 gold badges238 silver badges351 bronze badges asked Jul 5, 2012 at 0:49 user181275user181275 131 gold badge1 silver badge6 bronze badges 2
  • 1 don't use reserved words as variables, window is already an object. – balexandre Commented Jul 5, 2012 at 0:54
  • Thank you, I did not notice that. – user181275 Commented Jul 5, 2012 at 0:57
Add a ment  | 

1 Answer 1

Reset to default 13

You're shadowing the window global variable. Rename your variable:

var windowWidth = $( window ).width();
发布评论

评论列表(0)

  1. 暂无评论