jquery - 未捕获的类型错误:无法读取函数中未定义的属性“左侧”
问题描述
我有以下代码可以正常工作
$('#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();
});
谁能看到我做错了什么
解决方案
您是否尝试过在所有情况下将 (this) 换成 $('#styleList') ?
推荐阅读
- opencv - 如何在 bazel BUILD 脚本文件中添加 *.i 文件
- android - Android 应用程序中的表情符号按钮
- git - 有没有办法通过更改 .gitmodules 文件并提交该更改来更改 git 子模块指向的提交?
- r - 在使用 sf 包的 st_combine/st_union 之后,想要在 R 中获得干净的多边形?
- java - 如何在行中进行布局?
- c# - C# 字符串插值。十进制时使用字符串?一片空白
- node.js - geojson中的草皮和不扭结多边形
- javascript - 如何使用 javascript 获取无效的时间值?
- javascript - 如何用 2 个嵌套对象展平数组并将其变为一个
- c# - 如何测试我的 stacktrace walker 是否正确跳过库代码