![](/img/trans.png)
[英]setTimeout() function doesn't work inside of jQuery each() function
[英]css change inside setTimeout doesn't work
setTimeout中的以下代碼無法正常工作,而沒有setTimeout的相同代碼則可以正常工作
var size_disabled_input = 0;
$('#txtUSLead , #txtmy').on("mouseover", function () {
size_disabled_input = $(this).css('width');
if ((this.value.length) > 8)
{
$(this).css('cssText', 'width: ' + ((this.value.length + 1) * 7) + 'px !important');
}
});
$('#txtUSLead, #txtmy').on("mouseout", function () {
setTimeout(function (){
$(this).css('cssText', 'width: ' + size_disabled_input + 'px !important');
}, 2000);
})
在setTimeout
函數中, this
將不引用您所在的按鈕。
因此,您可以使用bind
方法:
$('#txtUSLead, #txtmy').on("mouseout", function () {
setTimeout(function () {
$(this).css('cssText', 'width: ' + size_disabled_input + 'px !important');
}.bind(this), 2000);
})
或者,使用變量存儲此值:
$('#txtUSLead, #txtmy').on("mouseout", function () {
var that = $(this);
setTimeout(function () {
that.css('cssText', 'width: ' + size_disabled_input + 'px !important');
}, 2000);
})
或者,您可以在jQuery中使用proxy()
方法:
$('#txtUSLead, #txtmy').on("mouseout", function () {
setTimeout($.proxy(function() {
$(this).css('cssText', 'width: ' + size_disabled_input + 'px !important');
}, this), 2000);
})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.