javascript 和 jshint,未定义

标签 javascript jshint defined

今天我写了这段代码:

(function (window) {
    'use strict';

    function ViewPort() {
        var getSize = function () {
                var e = window,
                    a = 'inner';

                if (!('innerWidth' in window)) {
                    a = 'client';
                    e = Document.documentElement || Document.body;
                }
                return { width : e[a + 'Width'], height : e[a + 'Height'] };
            },

            update = function () {
                var vw = (getSize().width / 100);

                Document.querySelector('html').style.fontSize = vw + 'px';
            };

        Document.addEventListener("resize", update());
    }

    function run() {
        return new ViewPort();
    }

    window.viewport = run;
}(window));

window.onload = function () {
    'use strict';
    viewport();
};

当我使用 jshint 时,出现如下错误:

11  'Document' is not defined. (W117)   e = Document.documentElement || Document.body;
19  'Document' is not defined. (W117)   Document.querySelector('html').style.fontSize = vw + 'px';
22  'Document' is not defined. (W117)   Document.addEventListener("resize", update());
30  'window' is not defined. (W117) }(window));
32  'window' is not defined. (W117) window.onload = function () {
34  'viewport' is not defined. (W117)   viewport();

任何人都可以帮助我解决我的错误吗?我不知道如何修复它,scripts.js 文件中的这段代码不是内联的。

最佳答案

Javascript 区分大小写。 document 全部小写。那么它应该可以工作。

如果仍然不起作用,您可以尝试在文件开头添加一条指令,以暗示该脚本适用于浏览器,并且浏览器全局变量可用。

将此注释添加到脚本顶部:

/* jshint browser: true */

关于javascript 和 jshint,未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28193526/

相关文章:

javascript - [allchars] 的正则表达式

javascript - JSHint 选项 "undef"有什么作用?

C# - 检查属性是否存在于枚举的元素上

javascript - Apache .htaccess 目录索引与 html5 视频

javascript - 在 java 类响应之前触发 Ajax 成功函数

javascript - 根据 jshint 的错误检测而无需进一步检查,将 == 更改为 === 是否安全

jshint - webpack.DefinePlugin 和 jshint "is not defined"

perl - 在一段时间内readdir()产生的 “0”如何不为假?

c++ - 类模板,允许用户定义类型 C++

javascript - 失去焦点时关闭即时验证