【问题标题】:What's the proper way to use jQuery in conjunction with 'use strict'?将 jQuery 与“use strict”结合使用的正确方法是什么?
【发布时间】:2012-05-03 01:05:36
【问题描述】:

如果我有类似以下的情况

"use strict";

$(document).ready(function () {

});

我收到警告

'$'is not defined

【问题讨论】:

  • 你的问题标题暗示了这一点,但如果你删除“use strict”指令,代码是否有效?

标签: javascript jquery use-strict


【解决方案1】:
( function ( $ ) {
    'use strict';
    $( document ).ready( function () {
        console.log( 'working!' )
    })
} ( jQuery ) )

【讨论】:

    【解决方案2】:

    您是否将 jQuery 添加到您的网站?

    在 tinker.io 上尝试相同的代码会产生错误,但随后添加 jQuery 库,错误就会消失。

    CDN:http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js

    使用这个sn-p添加它

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-10
      • 2016-06-11
      • 1970-01-01
      • 2018-04-10
      • 2013-01-02
      • 1970-01-01
      相关资源
      最近更新 更多