【问题标题】:Get class property from jquery callback从 jquery 回调中获取类属性
【发布时间】:2020-11-15 11:36:17
【问题描述】:

我有一堂课:

class MyClass{
    #arr= [];


    constructor(){
        $("body").on("click", ".myClass", function(e){
           //here, i need to access this.#arr, but unfortunatelly "this" no longer represents MyClass.
        }
    }
}

我该怎么做呢?提前致谢!

【问题讨论】:

    标签: javascript jquery class oop class-method


    【解决方案1】:

    使用箭头函数来保留this 值,之后您可以使用e.target 来获取被点击的元素。

    $("body").on("click", ".myClass", e=>{
    
    });
    

    【讨论】:

    • 是的,我想到了,但是我怎样才能获得点击的元素呢?
    • @kriskotooBG e.target.
    • 嗯,我不知道那是一回事。谢谢一百万!
    • @kriskotooBG 很高兴为您提供帮助。
    猜你喜欢
    • 1970-01-01
    • 2010-10-05
    • 2021-05-13
    • 1970-01-01
    • 2021-12-04
    • 2011-12-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-14
    相关资源
    最近更新 更多