在事件委托中使用箭头函数产生错误

$("#tempbox").on("click","#del",function(){
        var id = $(this).parents("tr").attr("data-id")
        $.ajax({
            type: "get",
            url: "http://127.0.0.1:5001/delhero/"+id,
            success: function (res) {
                if(res.status===200){
                    render();
               }
           }
      })
  })

 如上使用普通函数是没有问题的 打印出的$(this)为w.fn.init [button#del.button.ui.red];

    $("#tempbox").on("click","#del",()=>{
            console.log($(this));
            
            var id = $(this).parents("tr").attr("data-id")
            $.ajax({
                type: "get",
                url: "http://127.0.0.1:5001/delhero/"+id,
                success: function (res) {
                    if(res.status===200){
                        render();
                    }
                }
            })
        })

如上使用箭头函数打印出的$(this)为w.fn.init [document];

结论  凡是在选择函数的使用上务必要分清this的指向!

猜你喜欢

转载自blog.csdn.net/yu3jian4/article/details/84716630