gpt4 book ai didi

javascript - 用 typescript 转换 javascript 事件 this

转载 作者:行者123 更新时间:2023-12-03 06:47:57 26 4
gpt4 key购买 nike

我正在将以前的 JavaScript 代码转换为 Typescript。现在我遇到了以下问题。当我将旧原型(prototype)代码转换为 typescript 内的类和方法时,我遇到了事件问题:考虑以下代码:

// call this class, provide the parameter of the column
var self = this;
cell.onclick = function () {
self.makeTableSort(this); <== problem with parameter this.
};

我遇到的问题是我需要被单击的 html 元素的对象。其中“this”作为参数不是指类对象而是指 html 元素。

编辑:我已经尝试过如下所示:

// call this class, provide the parameter of the column
cell.onclick = (event) => {
this.makeTableSort(); <== missing parameter,
};

如何将其转换为 TypeScript?

最佳答案

试试这个:

cell.onclick = (event) => {
this.makeTableSort(event.currentTarget);
};

关于javascript - 用 typescript 转换 javascript 事件 this,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37638963/

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