扫码关注官方订阅号
遇到这么一个问题,如果把$(this)写在setTimeout里,就失效了,有什么办法可以提前保存$(this)吗?
走同样的路,发现不同的人生
可以使用 $.proxy(function, context) 来保持了特定的上下文(context )语境。修改后如下:
$.proxy(function, context)
$(document).on('mousedown', '#piece', function(e){ timeOut = setTimeout($.proxy(function(){ $(this).detach(); }, this), 1000); });
下面用$(this)的地方直接用this_temp不就对了
$(this)
this_temp
你不是已经保存到this_temp里了。用它就行了
你已经把$(this)存为this_temp了,下次直接用就可以了
SetTimeout的function的this已经改变了,用箭头函数吧
你不是已经保存了么?this_temp
可以啊,你已经定义了一个this_temp变量指向this ,你直接用this_temp就相当于在调用this了
$("body").on("click", function(){ var timeout = setTimeout(function(){ console.log($(this).attr("class")); }.call(this), 1000); });
微信扫码关注PHP中文网服务号
QQ扫码加入技术交流群
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
PHP学习
技术支持
返回顶部
可以使用
$.proxy(function, context)来保持了特定的上下文(context )语境。修改后如下:下面用
$(this)的地方直接用this_temp不就对了你不是已经保存到this_temp里了。用它就行了
你已经把$(this)存为this_temp了,下次直接用就可以了
SetTimeout的function的this已经改变了,用箭头函数吧
你不是已经保存了么?this_temp
可以啊,你已经定义了一个this_temp变量指向this ,你直接用this_temp就相当于在调用this了