admin管理员组文章数量:1320621
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
1 Answer
Reset to default 13You're shadowing the window
global variable. Rename your variable:
var windowWidth = $( window ).width();
本文标签: javascriptWhy is the width of the window nullStack Overflow
版权声明:本文标题:javascript - Why is the width of the window null? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742076872a2419458.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论