JSHint和jQuery:'$'没有定义 [英] JSHint and jQuery: '$' is not defined

查看:511
本文介绍了JSHint和jQuery:'$'没有定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下JS:

(function() {
  "use strict";

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

}());

收益率:

test.js: line 5, col 3, '$' is not defined.

使用JSHint 0.5.5进行linted时。任何想法?

When linted using JSHint 0.5.5. Any ideas?

推荐答案

如果您使用的是相对较新版本的JSHint,通常首选的方法是创建.jshintrc文件在项目的根目录中,并将此配置放入其中:

If you are using a relatively recent version of JSHint, the generally preferred approach is to create a .jshintrc file in the root of your project, and put this config in it:

{
    "globals": {
        "$": false
    }
}

这声明给JSHint $是一个全局变量,false表示它不应该被覆盖。

This declares to JSHint that $ is a global variable, and the false indicates that it should not be overridden.

真正旧版本的JSHint不支持.jshintrc文件(例如v0 .5.5喜欢2012年的原始问题)。如果您不能或不想使用.jshintrc文件,可以在脚本文件的顶部添加:

The .jshintrc file was not supported in really old versions of JSHint (such as v0.5.5 like the original question in 2012). If you cannot or do not want to use the .jshintrc file, you can add this at the top of the script file:

/*globals $:false */

还有一个简写的jqueryjshint选项,如上所示 JSHint选项页面 ..

There is also a shorthand "jquery" jshint option as seen on the JSHint options page..

这篇关于JSHint和jQuery:'$'没有定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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