JSHint and jQuery: '$' is not defined

前端 未结 9 1996
青春惊慌失措
青春惊慌失措 2020-12-07 07:13

The following JS:

(function() {
  \"use strict\";

  $(\"#target\").click(function(){
    console.log(\"clicked\");
  });

}());

Yields:

相关标签:
9条回答
  • 2020-12-07 08:09

    All you need to do is set "jquery": true in your .jshintrc.

    Per the JSHint options reference:

    jquery

    This option defines globals exposed by the jQuery JavaScript library.

    0 讨论(0)
  • 2020-12-07 08:12

    You can also add two lines to your .jshintrc

      "globals": {
        "$": false,
        "jQuery": false
      }
    

    This tells jshint that there are two global variables.

    0 讨论(0)
  • 2020-12-07 08:14

    You probably want to do the following,

    const $ = window.$
    

    to avoid it throwing linting error.

    0 讨论(0)
提交回复
热议问题