提问人:bob 提问时间:10/26/2023 更新时间:10/26/2023 访问量:36
为什么我有jquery错误预期赋值或函数调用,而是看到一个表达式?
Why am I having the jquery error Expected an assignment or function call and instead saw an expression?
问:
我遇到错误 - 预期是赋值或函数调用,而是看到了一个表达式。它是用jQuery自定义插件代码编写的。谁能帮忙?返回在功能主体中既不起作用
(function ($) {
$.fn.moveAnimate = function () {
$(".move").animate(
{
position: "absolute",
left: "150px"
},
1000
);
};
});
答:
0赞
Isonimus
10/26/2023
#1
这是因为您尝试直接在插件定义中使用 jQuery 的 animate 方法。要正确定义自定义 jQuery 插件,您应该返回一个函数,该函数将在调用插件时执行。此外,您应该致电 IIFE:
(function ($) {
$.fn.moveAnimate = function () {
return this.each(function () {
// Inside this function, "this" refers to each element matched by the selector
var $element = $(this);
$element.animate(
{
position: "absolute",
left: "150px"
},
1000
);
});
};
})(jQuery);
然后,您可以在选择的元素上使用它,如下所示:
$(document).ready(function () {
$(".move").moveAnimate();
});
评论
0赞
bob
10/28/2023
实际上,现在没有定义moveAnimate。
评论