javascript - $(窗口).width();使用严格模式

标签 javascript jquery

$(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 。这可能会停止您的代码的执行

From the documentation

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/

相关文章:

javascript - 如何在 Highcharts 中修改 Column stacked percent Chart

javascript - 在普通javascript对象中将 child 分组在 parent 之下?

javascript - 在父 div 上使用 .height() 后,改变高度的子元素不再影响父元素的高度,为什么?

javascript - jQuery - 通过按钮增加和减少输入值

javascript - PEGJS:如何将 NOT (!) 逻辑运算符添加到解析 AND (&&) OR (||) 逻辑语句的语法中

javascript - 当 console.assert 失败时进入 chrome 调试器

javascript - 为什么在将用户输入数据库时​​出现错误?

javascript - 如果只有一行文本,则垂直居中文本区域文本

jquery - 如何覆盖 select : within jQuery UI Autocomplete? 的默认行为

javascript - 分组和取消分组 Fabric.js 对象