Jquery 在我包含严格模式之前,代码工作正常
Jquery code working fine until I included strict mode
一段 Jquery 代码工作得很好,直到 "superiors" 告诉我必须在我的所有 Javascript 代码中包含严格模式。除了这段代码外,我的 main.js 文件一切正常。我无法重新识别错误,但每次激活脚本时都会触发我的控制台错误,它是一个 Boostrap 4 切换按钮,它确实打开了一个菜单,但菜单从上到下打开并且非常缓慢。按钮应该从右向左折叠。
$(function(){
// mobile menu slide from the left
$('[data-toggle="collapse"]').on('click', function() {
$navMenuCont = $($(this).data('target'));
$navMenuCont.animate({'width':'toggle'}, 280);
});
})
控制台报错如下:
Uncaught ReferenceError: $navMenuCont is not defined
at HTMLButtonElement.<anonymous> (main.js:61)
at HTMLButtonElement.dispatch (jquery-3.3.1.js:5183)
at HTMLButtonElement.elemData.handle (jquery-3.3.1.js:4991)
来自 MDN 参考:
First, strict mode makes it impossible to accidentally create global
variables. In normal JavaScript mistyping a variable in an assignment
creates a new property on the global object and continues to "work"
(although future failure is possible: likely, in modern JavaScript).
Assignments, which would accidentally create global variables, instead
throw an error in strict mode.
您需要声明您的 $navMenuCont
变量。
例如:
var $navMenuCont = $($(this).data('target'));
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode
一段 Jquery 代码工作得很好,直到 "superiors" 告诉我必须在我的所有 Javascript 代码中包含严格模式。除了这段代码外,我的 main.js 文件一切正常。我无法重新识别错误,但每次激活脚本时都会触发我的控制台错误,它是一个 Boostrap 4 切换按钮,它确实打开了一个菜单,但菜单从上到下打开并且非常缓慢。按钮应该从右向左折叠。
$(function(){
// mobile menu slide from the left
$('[data-toggle="collapse"]').on('click', function() {
$navMenuCont = $($(this).data('target'));
$navMenuCont.animate({'width':'toggle'}, 280);
});
})
控制台报错如下:
Uncaught ReferenceError: $navMenuCont is not defined
at HTMLButtonElement.<anonymous> (main.js:61)
at HTMLButtonElement.dispatch (jquery-3.3.1.js:5183)
at HTMLButtonElement.elemData.handle (jquery-3.3.1.js:4991)
来自 MDN 参考:
First, strict mode makes it impossible to accidentally create global variables. In normal JavaScript mistyping a variable in an assignment creates a new property on the global object and continues to "work" (although future failure is possible: likely, in modern JavaScript). Assignments, which would accidentally create global variables, instead throw an error in strict mode.
您需要声明您的 $navMenuCont
变量。
例如:
var $navMenuCont = $($(this).data('target'));
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode