什么是原始的JavaScript相当于jQuery的$(window).width

hyb*_*rid 5 jquery width

我听说它是offset.width,document.documentElement.clientWidth和window.innerWidth

我很好奇我不能使用jQuery的项目,我应该使用哪种解决方案?

Ale*_*tie 5

function windowWidth() {
    var docElemProp = window.document.documentElement.clientWidth,
        body = window.document.body;
    return window.document.compatMode === "CSS1Compat" && docElemProp || body && body.clientWidth || docElemProp;
}
Run Code Online (Sandbox Code Playgroud)

拍摄(并从jQuery源进行了稍微修改:

https://github.com/jquery/jquery/blob/master/src/dimensions.js#L42