这两种声明之间有什么区别吗?我想使用其中之一,以解决一些css问题的移动版本的响应网站。一些教程建议使用$(document).width()
,而另一些则建议使用document.body.clientWidth
。我知道第一个是jquery,第二个是普通的JavaScript,但除此之外还有什么区别吗?哪一种更适合使用?
我想用这种方法:
if ($(document).width() < 768) { ... }
或
if (document.body.clientWidth < 768) { ... }
发布于 2013-11-19 11:33:27
是的,它们是不同的。jQuery做了很多事情来试图使结果正常化。
请参阅源代码
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]);
}
发布于 2015-09-02 22:24:32
document.body.clientWidth =$(文档).width()+(左填充+右填充)
https://stackoverflow.com/questions/20069744
复制相似问题