首页 > 解决方案 > 未捕获的类型错误:无法读取函数中未定义的属性“左侧”

问题描述

我有以下代码可以正常工作

$('#btnSort').on("click",
        function () {
            $("#sortorder").css({
                'position': 'absolute',
                'z-index': '99',
                'left': $(this).offset().right,
                'top': $(this).offset().top + $(this).height() + 5
            }).slideToggle().delay(1000);
        });

我正在尝试检测浏览器窗口的大小,因此已将代码放入函数中,但现在出现错误:

未捕获的类型错误:无法读取未定义的属性“左侧”

下面是我的功能

var width = $(window).width();
    $.fn.positionFunction = function () {
        if (width < 500) {
            $("#styleList").css({
                'position': 'absolute',
                'z-index': '99',
                'left': $(this).offset().right,
                'top': $(this).offset().top + $(this).height() + 5
            }).slideToggle(1000);

        } else {
            $("#styleList").css({
                'position': 'absolute',
                'z-index': '99',
                'left': $(this).offset().left, <----------Error
                'top': $(this).offset().top + $(this).height() + 5
            }).slideToggle(1000);
        }
    }

我正在尝试使用以下方法调用它:

$('#btnStyle').on("click", function() {
        $.fn.positionFunction();
    });

谁能看到我做错了什么

标签: jquery

解决方案


您是否尝试过在所有情况下将 (this) 换成 $('#styleList') ?


推荐阅读