未定义 Javascript 文档

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29211586/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 02:59:44  来源:igfitidea点击:

Javascript document is not defined

javascriptjquerydomdocument

提问by Ary

I have this error and I don't understand why: ReferenceError: document is not defined. I tried to put window.content.document, but error windowis not defined too. I don't know where is the trick and specially I don't understand why? Worst when I take it off, I have as error ReferenceError: $ is not definedwhereas I included jQuery in my html.

我有这个错误,我不明白为什么: ReferenceError: document is not defined. 我尝试放置window.content.document,但window也未定义错误。我不知道诀窍在哪里,特别是我不明白为什么?最糟糕的是,当我取下它时,我ReferenceError: $ is not defined在我的 html 中包含 jQuery 时出现错误。

this is my script.js:

这是我的 script.js:

var client = {};

client.start = function (data) {

    setTimeout(function(data) {
    client.chart(data);
    }, 60);
};

module.exports.getJson= function(data){

    client.start(data);
};

client.chart= function (data) {

    //console.log(" this is the data" + data);

    $(document).ready(function() {

        $(function () {
          //Do my stuff
        });
    });
};

and my html:

和我的 html:

<!doctype html>
<html lang="en">
  <head>
    <meta charset="UTF-8">


    <script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
    <script language="JavaScript" type="text/javascript" src="/js/jquery-1.2.6.min.js"></script>
    <script language="JavaScript" type="text/javascript" src="/js/sprinkle.js"></script>
    <script src="http://code.highcharts.com/highcharts.js"></script>
    <script src="http://code.highcharts.com/modules/exporting.js"></script>

    <script type="text/javascript" src="script.js"></script>

  </head>
  <body>
    <div id="containerChart" style="min-width: 310px; height: 400px; margin: 0 auto"></div>
  </body>
</html>

Any ideas?

有任何想法吗?

采纳答案by Bhushan Kawadkar

You cannot have documentobject inside your script so remove below code from script.js

document的脚本中不能有对象,因此请从 script.js 中删除以下代码

$(document).ready(function() {
    $(function () {
      //Do my stuff
    });
});

Instead of above code, you can call your script function from html page like below

您可以从 html 页面调用脚本函数,而不是上面的代码,如下所示

<script>
    $(document).ready(function() {
      //call your script.js function from here
    });
</script>

回答by Charlie-Greenman

The proper answer, I believe, is that the linter you are using, does not know that you are running in a browser environment. Objects such as document and window are predefined. Personally, I had this issue with eslint and I fixed it by doing the following:

我相信,正确的答案是您使用的 linter 不知道您正在浏览器环境中运行。文档和窗口等对象是预定义的。就我个人而言,我在使用 eslint 时遇到了这个问题,我通过执行以下操作来解决它:

env: {
        browser: true
      }

Hope this helps somewhat.

希望这会有所帮助。