Error javascript



在控制台中,我有此错误。我使用最新的jquery 3.1。

如何纠正此元素?

谢谢

typeError: window.on 不是函数

<script>(window).on('load', function (){var maxHeight=0;$(".equal-height").each(function(){if($(this).height()>maxHeight){maxHeight=$(this).height();}});$(".equal-height").height(maxHeight);});</script>
您需要

使用 $ 作为脚本(window)部分的前缀,否则它不会是 JQuery 对象。

<script>$(window).on('load', function (){var maxHeight=0;$(".equal-height").each(function(){if($(this).height()>maxHeight){maxHeight=$(this).height();}});$(".equal-height").height(maxHeight);});</script>

你在代码的开头缺少$符号,这是jQuery的简写,在使用任何jQuery库时都需要使用。您的完整代码变为:

$(window).load(function() {
    var maxHeight = 0;
    $(".equal-height").each(function() {
        if ($(this).height() > maxHeight) {
            maxHeight = $(this).height();
        }
    });
    $(".equal-height").height(maxHeight);
};

这也与

jQuery(window).load(function() {
    var maxHeight = 0;
    $(".equal-height").each(function() {
        if ($(this).height() > maxHeight) {
            maxHeight = $(this).height();
        }
    });
    $(".equal-height").height(maxHeight);
};

最新更新