使用HTML运行JSHint并出现错误 [英] Running JSHint with HTML and getting errors

查看:67
本文介绍了使用HTML运行JSHint并出现错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是JSHint的新手,正在尝试运行它来验证我的Javascript.我安装了node.js,当我尝试运行JSHint时,出现以下错误.

I'm a newbie to JSHint and trying to run it to validate my Javascript. I have node.js installed and when i try to run JSHint i get the error below.

C:\Users\574839\Desktop\testscr.js:1
(function (exports, require, module, __filename, __dirname) { <!DOCTYPE html>
                                                          ^
SyntaxError: Unexpected token <
at Object.exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:513:28)
at Object.Module._extensions..js (module.js:550:10)
at Module.load (module.js:458:32)
at tryModuleLoad (module.js:417:12)
at Function.Module._load (module.js:409:3)
at Function.Module.runMain (module.js:575:10)
at startup (node.js:160:18)
at node.js:456:3

我有一个带有以下选项的.jshintrc文件:

I have a .jshintrc file with the following options:

{
 "bitwise": true,
 "camelcase": true,
 "curly": true,
 "eqeqeq": true,
 "es3": false,
 "forin": true,
 "freeze": true,
 "immed": true,
 "indent": 4,
 "latedef": "nofunc",
    "newcap": true,
    "noarg": true,
  "noempty": true,
  "nonbsp": true,
  "nonew": true,
 "plusplus": false,
 "quotmark": "single",
 "undef": true,
 "unused": false,
 "strict": false,
 "maxparams": 10,
 "maxdepth": 5,
 "maxstatements": 40,
 "maxcomplexity": 8,
 "maxlen": 120,
 "asi": false,
 "boss": false,
 "debug": false,
 "eqnull": true,
 "esnext": false,
 "evil": false,
 "expr": false,
 "funcscope": false,
 "globalstrict": false,
 "iterator": false,
 "lastsemic": false,
 "laxbreak": false,
 "laxcomma": false,
 "loopfunc": true,
 "maxerr": false,
 "moz": false,
 "multistr": false,
 "notypeof": false,
 "proto": false,
 "scripturl": false,
 "shadow": false,
 "sub": true,
 "supernew": false,
 "validthis": false,
 "noyield": false,
 "browser": true,
 "node": true,
 "globals": {
     "angular": false,
   "$": false
  }
}

有人可以告诉我为什么会出错吗?

Can someone please advise why I get the error?

我的JS代码在下面.

<!DOCTYPE html>
<html>
<body>

<h1>What Can JavaScript Do?</h1>

<p id="demo">JavaScript can change HTML content.</p>

<button type="button" onclick="document.getElementById('demo').innerHTML='Hello JavaScript!'">Click Me!</button>

</body>
</html>

推荐答案

这是HTML文件,而不是JavaScript文件.但是幸运的是,如果您将--extract选项传递给它,则JSHint可以在HTML文件中的javascript上工作​​.

That's an HTML file and not a JavaScript file. But luckily JSHint can work on the javascript from HTML files if you pass the --extract option to it.

文档:

--extract=[auto|always|never]

告诉JSHint在插入之前从HTML文件中提取JavaScript:

--extract=[auto|always|never]

Tells JSHint to extract JavaScript from HTML files before linting:

tmp ☭ cat test.html
<html>
  <head>
    <title>Hello, World!</title>
    <script>
      function hello() {
        return "Hello, World!";
      }
    </script>
  </head>
  <body>
    <h1>Hello, World!</h1>
    <script>
      console.log(hello())
    </script>
  </body>
</html>

tmp ☭ jshint --extract=auto test.html
test.html: line 13, col 27, Missing semicolon.

1 error

如果将其始终设置为 ,JSHint将始终尝试提取 JavaScript.如果将其设置为 auto ,则只会尝试 如果文件看起来像是HTML文件.

If you set it to always JSHint will always attempt to extract JavaScript. And if you set it to auto it will make an attempt only if file looks like it's an HTML file.

同时,在这种情况下,它仍然无法识别HTML属性事件处理程序(如onclick)中的javascript,这是一种不好的做法.如果您确实希望将Javascript包含在HTML中,则可以将其放入脚本标签中,如下所示:

In the mean time, it still doesn't recognize the javascript in HTML attribute event handlers like onclick such as this case, which is a bad practice. If you really want to have your Javascript in HTML you have put it in script tags such as the following:

testscr.html:

<!DOCTYPE html>
<html>
    <body>

    <h1>What Can JavaScript Do?</h1>

    <p id="demo">JavaScript can change HTML content.</p>

    <button type="button" onclick="sayHello()">Click Me!</button>
    <script>
      function sayHello() {
        document.getElementById('demo').innerHTML='Hello JavaScript!';
      }
    </script>
    </body>
</html>

另外,就像您将其传递给节点一样,如node testscr.js,但是您应该做的是将其传递给jshint:jshint --extract=always testscr.js

Also it looks like you're passing it to node, like node testscr.js, but what you should be doing is to pass it to jshint: jshint --extract=always testscr.js

这篇关于使用HTML运行JSHint并出现错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆