小编Swa*_*gwa的帖子

为什么 JQuery $(this) 在箭头函数中不起作用

我正在尝试使用 $(this) 以便我可以在箭头中使用 jQuery 对象,但它不起作用

$('.btnReserve').click(function(){
        var element_div = $(this).closest('div');
        console.log(element_div.attr('id');
    });
Run Code Online (Sandbox Code Playgroud)

效果很好

但...

$('.btnReserve').click(() => {
        var element_div = $(this).closest('div');
        console.log(element_div.attr('id');
    });
Run Code Online (Sandbox Code Playgroud)

$(this) 不起作用

为了获得所需的结果,我使用了以下方法

$('.btnReserve').click((event) => {
        var element_div = $(event.target).closest('div');
        console.log(element_div.attr('id');
    });
Run Code Online (Sandbox Code Playgroud)

jquery ecmascript-6 arrow-functions

0
推荐指数
1
解决办法
588
查看次数

标签 统计

arrow-functions ×1

ecmascript-6 ×1

jquery ×1