gpt4 book ai didi

javascript - IE11 中的 jQuery - $(document).width() 和 $ ("html").width() 之间的区别

转载 作者:行者123 更新时间:2023-11-27 23:48:29 25 4
gpt4 key购买 nike

所以我在 IE11 中有这个...

四舍五入(我认为...)

console.log($(document).width()); // 808px;

不圆

console.log($("html")[0].getBoundingClientRect().width); // 807.30993px;

向下舍入(或最接近的整数)

console.log($("html").width()); //807px;

显然,$(document) 和 $("html") 是相同的,并且 jQuery 将 html 元素向下舍入而不是像浏览器那样向上舍入。或者 IE11 为 $(document) 提供了一些额外的空间,使像素数量比 html 更高?

我以为 html 应该代表整个文档?

我的问题:

它们应该是一样的宽度吗?就像在 Firefox、Chrome 等中一样。

最佳答案

这是对类似问题的回答: In JavaScript, the document keyword is a handle on the object that contains the HTMLDocument.所以他们不一样, document 是一个 object ,它也包含页面的html。

Now when you get the width from the html and document.

console.log($("html").width());

为您提供 html 的宽度。

console.log($(document).width());为您提供文档可见部分的宽度,因此宽度显然为 <html>本身,但如果您对 html 应用了 margin ,则存在差异。 , 那么文档的宽度是 sum of width of html and the margin applied to it .

查看此示例 http://jsfiddle.net/bbke9n59/

<div style='background:red;height:30px;'></div>

html{
margin:20px;
}

在这里,在我的浏览器中,我得到了,

console.log('html='+$("html").width()); // width 630

console.log('document='+$(document).width());// width 670

确切地说,宽度有 40 像素的差异,那只是应用于 html 的边距

现在这都是关于 chrome 和 firefox 的,

About IE

IE-9当我在 IE-9 上运行相同的页面时

   console.log('html='+$("html").width()); // width 570

console.log('document='+$(document).width());// width 570

html和document的宽度没有区别(真的应该有)

IE-11当我在 IE-11 上运行相同的页面时

   console.log('html='+$("html").width()); // width 517

console.log('document='+$(document).width());// width 557

40 的精确差异。正如 chrome 和 firefox 显示的那样。

所以我不再能够重现四舍五入的问题(在任何浏览器中),所以对我来说,我想问题不在于使用 jquery 进行四舍五入(如果那是问题,那么 jquery 会将 documenthtml 的宽度四舍五入,并且仍然使它们具有相同的宽度)

关于javascript - IE11 中的 jQuery - $(document).width() 和 $ ("html").width() 之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28494511/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com