gpt4 book ai didi

javascript - 使用 setTimeOut 后函数不显示父类 - jQuery

转载 作者:行者123 更新时间:2023-11-28 13:00:54 25 4
gpt4 key购买 nike

在显示父元素类的函数中使用 setTimeout 后,console.log 中的结果未定义。

在用户触发输入模糊后,如何对类进行检查?

jsfiddle

    <div class="field-wrapper field-pristine">
<input type="text" class="field-item" value="test">
</div>

jQuery('body').on('blur', '.field-wrapper input.field-item', function() {
setTimeout(function(){
console.log($( this ).closest('.field-wrapper').attr('class'));
}, 1200);
})

最佳答案

您正在使用该函数丢失 this 指针,您可以使用不绑定(bind)到 this 的箭头函数

jQuery('body').on('blur', '.field-wrapper input.field-item', function () {
setTimeout(() => {
console.log($(this).closest('.field-wrapper').attr('class'));
}, 1200);
})

关于javascript - 使用 setTimeOut 后函数不显示父类 - jQuery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50607828/

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