$(window).width() 与 $(document).width() 之间的区别
jQuery 中 $(window).width()
与 $(document).width()
之间的主要区别是什么? window是否代表浏览器,document是否代表html页面的主体?我说得对吗?
What is the major difference between $(window).width()
vs $(document).width()
in jQuery?
Whether window denotes the browser and document represents the body of html page? Am I correct ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
来自
width()
的文档:简单的 jsFiddle 演示
在演示中,我设置了
html { width: 1000px; }
,它比视口大。HTML 页面主体的宽度是第三个值。
$('body').width()
也可以与其他两个不同(例如尝试body { margin: 100px; }
)。From the documentation of
width()
:Simple jsFiddle Demo
In the demo, I have set
html { width: 1000px; }
, which is bigger than the viewport.The width of the body of your HTML page is a third value.
$('body').width()
can also differ from the other two (trybody { margin: 100px; }
for example).你是对的。
window
是浏览器的可视区域。document
是页面的实际主体。因此您的文档
可以远远超出窗口
You are correct. the
window
is the viewable area of the browser. Thedocument
is the actually body of the page. So yourdocument
could extend far beyond thewindow
所以总而言之,文档可能比窗口小。
来自:http://eligeske。 com/jquery/document-and-window-objects-2/之间的差异是什么/
So in conclusion the document could be smaller than the window.
FROM: http://eligeske.com/jquery/what-is-the-difference-between-document-and-window-objects-2/
$(window).width();
返回浏览器视口的宽度$(document).width();
返回 HTML 文档的宽度$(document) .width()
有点不可靠,导致全屏浏览器的值较低。 $(window).width() 更安全。$(window).width()
获取窗口的整个宽度,包括滚动条之类的内容。$(window).width();
returns the width of browser viewport$(document).width();
returns width of HTML document$(document).width()
is a bit unreliable, resulting in a lower value for a full-screened browser . $(window).width() is safer.$(window).width()
gets the entire width of the window, including things like the scroll bar .另一个重要的区别。
$(window).width();
在文档加载/准备好之前可用$(document).width();
仅在文档加载后可用所以对于第二个,你需要
Another important difference.
$(window).width();
is available before the document loads / is ready$(document).width();
is only available after the document had loadedSo for the second, you need
是 - 窗口宽度是浏览器窗口的宽度,文档宽度是网页的宽度。
Yes - width of window is width of browser window, and width of document is width of webpage.