$(window).width() 与 $(document).width() 之间的区别

发布于 2025-01-08 15:01:54 字数 136 浏览 4 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

睫毛溺水了 2025-01-15 15:01:54

来自 width() 的文档:

此方法还能够找到窗口和文档的宽度。

$(窗口).width(); // 返回浏览器视口的宽度
$(文档).width(); // 返回 HTML 文档的宽度

简单的 jsFiddle 演示

在演示中,我设置了 html { width: 1000px; },它比视口大。

HTML 页面主体的宽度是第三个值。 $('body').width() 也可以与其他两个不同(例如尝试 body { margin: 100px; })。

From the documentation of width():

This method is also able to find the width of the window and document.

$(window).width();   // returns width of browser viewport
$(document).width(); // returns width of HTML document

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 (try body { margin: 100px; } for example).

浅沫记忆 2025-01-15 15:01:54

你是对的。 window 是浏览器的可视区域。 document 是页面的实际主体。因此您的文档可以远远超出窗口

You are correct. the window is the viewable area of the browser. The document is the actually body of the page. So your document could extend far beyond the window

那些过往 2025-01-15 15:01:54

嗯,窗口是第一个加载到浏览器中的东西。
这个window对象具有大部分属性,例如length
innerWidthinnerHeightname,如果已关闭,则其父级以及
更多。

那么文档对象呢?

document 对象是您的 html 文档,它将被加载到
浏览器。 document 实际上被加载到 window 对象中
并具有可用的属性,如标题、URL、cookie 等。
这真的意味着吗?这意味着如果您想访问某处房产
window 则为 window.property,如果是 document 则为
window.document.property 也可以简称为
document.property.

所以总而言之,文档可能比窗口小。

来自:http://eligeske。 com/jquery/document-and-window-objects-2/之间的差异是什么/

Well, the window is the first thing that gets loaded into the browser.
This window object has the majority of the properties like length,
innerWidth, innerHeight, name, if it has been closed, its parents, and
more.

What about the document object then?

The document object is your html document that will be loaded into the
browser. The document actually gets loaded inside the window object
and has properties available to it like title, URL, cookie, etc. What
does this really mean? That means if you want to access a property for
the window it is window.property, if it is document it is
window.document.property which is also available in short as
document.property.

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/

余厌 2025-01-15 15:01:54

$(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 .

酸甜透明夹心 2025-01-15 15:01:54

另一个重要的区别。

$(window).width(); 在文档加载/准备好之前可用

$(document).width(); 仅在文档​​加载后可用

所以对于第二个,你需要

$(document).ready(function() {
   var w = $(document).width();
});

Another important difference.

$(window).width(); is available before the document loads / is ready

$(document).width(); is only available after the document had loaded

So for the second, you need

$(document).ready(function() {
   var w = $(document).width();
});
Smile简单爱 2025-01-15 15:01:54

是 - 窗口宽度是浏览器窗口的宽度,文档宽度是网页的宽度。

Yes - width of window is width of browser window, and width of document is width of webpage.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文