HTML:
<div>
<button data-id="3">Click Me</button>
</div>
In classic jQuery I would do:
$("div").on("click","button", test);
function test(){
alert($(this).data("id"));
}
To get the data-id
of the clicked element
In TypeScript (in a class) I use:
class foo { ...
$("div").on("click", "button", (event) => this.test());
public test(){
alert($(this).data("id")); // "undefined"
console.log($(this));
}
....
}
Here I don't get the clicked element - $(this)
is the instance of the class.
What did I do wrong?
question from:
https://stackoverflow.com/questions/22843255/typescript-using-jquery-this-in-event 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…