gpt4 book ai didi

插件函数内的 jQuery $(this)

转载 作者:行者123 更新时间:2023-12-03 22:40:56 25 4
gpt4 key购买 nike

我有这个 jQuery 插件:

$.fn.touchBind = function(func) {
$(this).live('touchmove', function() {
$(this).addClass('dragged');
});

$(this).live('touchend', function() {
if ($(this).hasClass('dragged') == false) {
func();
}
});

return this;
}

并这样调用它:

$('.the-element').touchBind(function() {
$(this).hide();
});

我的问题是 $(this).hide() 中的 $(this) 不引用 $('.the-element' ),而是 DOMWindow。有什么办法可以让我完成这项工作吗?

最佳答案

func(); 更改为 func.call(this);$.proxy(func, this)();

您还可以使用 apply() (当 call() 适合时不需要)或 bind() (有限的浏览器支持,$.proxy() 本质上做同样的事情)。

关于插件函数内的 jQuery $(this),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8176182/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com