[英]Missing “Use strict” statement
I am new to coding. 我是编码的新手。 I am receiving this error: "Missing "Use strict" statement" in my Javascript code. 我收到此错误:“Missing”在我的Javascript代码中使用严格的“语句”。 Here is my code: 这是我的代码:
$(function(){
$('.nav-toggle').on('click',function(){
$('.main-nav').toggleClass('open');
});
});
I do not know where to put 'use-strict';
我不知道把'use-strict';
放在哪里'use-strict';
. 。 Where do I put the statement? 我在哪里发表声明?
As Barmar said, the use strict is optional, but if you had to place it somewhere the best place I would say you should put it is 正如Barmar所说,use strict是可选的,但如果你不得不把它放在最好的地方我会说你应该把它放在哪里
$(function(){
"use strict";
$('.nav-toggle').on('click',function(){
$('.main-nav').toggleClass('open');
});
});
This is where you have to put it whether you use Jquery or Javascript 无论您使用Jquery还是Javascript,都必须使用它
(function($){ "use strict"; $('.nav-toggle').on('click',function(){ $('.main-nav').toggleClass('open'); }); })(jQuery);
.open{ display: none; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <div> <button class="nav-toggle">Open</button> <div class="main-nav"> <ul> <li>Home</li> <li>Contact</li> </ul> </div> </div>
Assuming you want to achieve a responsive navbar effect I have added the relevant code to make this easier. 假设您想要实现响应式导航栏效果,我添加了相关代码以使其更容易。
In the jQuery part, I'm creating an anonymous function and passing it the $
sign so you can use it safely without polluting the global scope. 在jQuery部分中,我正在创建一个匿名函数并将其传递给$
符号,以便您可以安全地使用它而不会污染全局范围。 "use strict"
use is not mandatory but if you want to use place it inside the anonymous function as I did. "use strict"
使用不是强制性的,但是如果你想使用它就像我一样将它放在匿名函数中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.