$(window).width();
函数在不使用 “use strict”
模式的情况下运行良好。但是,这不行。使用“use strict”
模式后。
这里出了什么问题?
(function($) {
"use strict";
$('#menu .nav > li').on("mouseover", function() {
$screensize = $(window).width();
if ($screensize > 991) {
$(this).find('> .dropdown-menu').stop(true, true).slideDown('fast');
}
$(this).bind('mouseleave', function() {
$screensize = $(window).width();
if ($screensize > 991) {
$(this).find('> .dropdown-menu').stop(true, true).css('display', 'none');
}
});
});
})(jQuery);
最佳答案
如果您使用严格模式,则需要确保使用 var
关键字声明 $screensize
,否则它会告诉您这是一个 undefined variable 。这可能会停止您的代码的执行
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 in strict mode
我建议您使用浏览器的控制台调试器并熟悉它。它应该抛出 Uncaught ReferenceError: $screensize is not Defined
关于javascript - $(窗口).width();使用严格模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37728990/