如何合理地使用jQuery和"use strict"?

9
如果我有以下内容:
"use strict";

$(document).ready(function () {

});

我收到了警告。
'$'is not defined

你的问题标题已经暗示了,但是如果你移除“use strict”指令,代码是否能够工作? - nnnnnn
2个回答

24
( function ( $ ) {
    'use strict';
    $( document ).ready( function () {
        console.log( 'working!' )
    })
} ( jQuery ) )

0
你在网站上添加了jQuery吗?
在tinker.io上尝试相同的代码会产生错误,但是添加jQuery lib后错误消失。
CDN:http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js 使用此片段添加它。
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接