您好,我的引用资料有问题。 例如:
class Inset{
constructor(){
this.x = 1;
this.y = 2;
this.divBox = $('<div></div>);
this.divBox.draggable({drag: function(event, ui){
//Here i want to call the add funciton with my x and y but i dont get it, how i can reference to the add function.
}})
}
add(x, y){
return x+y;
}
}
如何从 Inset 类中调用函数 add(x,y)?
最佳答案
试试这个:
JS
class Inset {
constructor(){
this.x = 1;
this.y = 2;
this.divBox = $('.draggable');
this.divBox.draggable({
drag: (event, ui) => {
this.add(10,20);
}
});
}
add(x, y){
console.log(x, y);
return x + y;
}
}
new Inset();
CSS
.draggable {
border: 2px solid #000;
height: 20px;
width: 20px;
}
HTML
<div class="draggable"></div>
关于javascript - 在 jQuery 的拖动事件中使用在类中定义的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51573625/