首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >$(document).width()与document.body.clientWidth

$(document).width()与document.body.clientWidth
EN

Stack Overflow用户
提问于 2013-11-19 11:02:48
回答 2查看 6.1K关注 0票数 4

这两种声明之间有什么区别吗?我想使用其中之一,以解决一些css问题的移动版本的响应网站。一些教程建议使用$(document).width(),而另一些则建议使用document.body.clientWidth。我知道第一个是jquery,第二个是普通的JavaScript,但除此之外还有什么区别吗?哪一种更适合使用?

我想用这种方法:

代码语言:javascript
运行
复制
if ($(document).width() < 768) { ... }

代码语言:javascript
运行
复制
if (document.body.clientWidth < 768) { ... }
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-11-19 11:33:27

是的,它们是不同的。jQuery做了很多事情来试图使结果正常化。

请参阅源代码

代码语言:javascript
运行
复制
if (elem.nodeType === 9) {
    doc = elem.documentElement;

    // Either scroll[Width/Height] or offset[Width/Height] or client[Width/Height], whichever is greatest
    // unfortunately, this causes bug #3838 in IE6/8 only, but there is currently no good, small way to fix it.
    return Math.max(
    elem.body["scroll" + name], doc["scroll" + name], elem.body["offset" + name], doc["offset" + name], doc["client" + name]);
}
票数 1
EN

Stack Overflow用户

发布于 2015-09-02 22:24:32

document.body.clientWidth =$(文档).width()+(左填充+右填充)

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20069744

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档