gpt4 book ai didi

javascript - 在 jQuery 的拖动事件中使用在类中定义的方法

转载 作者:行者123 更新时间:2023-12-03 01:25:25 25 4
gpt4 key购买 nike

您好,我的引用资料有问题。例如:

    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>

DEMO

关于javascript - 在 jQuery 的拖动事件中使用在类中定义的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51573625/

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