【发布时间】:2021-07-30 11:10:06
【问题描述】:
我想定义一个在 JSXGraph 小部件中使用的对象,它可以在鼠标单击时激活或停用(现在只是切换可见性和“固定”属性)。
我定义了方法activate() 和deactivate()。这些可以从构造方法中调用(根据参数设置初始状态)。 但是我无法将方法绑定到鼠标事件。
知道可能导致问题的原因或如何正确解决任务吗?
this.graph = board.create('functiongraph' [
JXG.Math.Numerics.hermitePolynomial(this.p1, this.p2, this.pt1, this.pt2,
this.t1, this.t2),
this.p1.X(), this.p2.X() ],
{ strokecolor: 'red', strokewidth: 3 });
// set activate/deactivate, this works
this.obj = [ this.p1, this.p2, this.pt1, this.pt2, this.t1, this.t2 ];
if (this.state == "active") { this.activate() }
else {this.deactivate() }
//switch by mouseclick, this doesn't work
this.graph.on('down', function() {
if (this.state == "active") {
console.log("deactivate", this.obj); this.deactivate(); }
else { console.log("activate"); this.activate(); }
} )
}
activate() {console.log("activate"); this.state = "active";
for (var part of this.obj) {
part.setAttribute({visible:true});
part.setAttribute({fixed:false});
} update()}
deactivate() {console.log("deactivate"); this.state = "inactive";
for (var part of this.obj) {
part.setAttribute({visible:false});
part.setAttribute({fixed:true});
} update()}
在这个例子中,右边的红色样条应该可以通过鼠标点击来切换。
但单击它们会导致错误
未捕获的类型错误:this.activate 不是函数
这个我没看懂,因为上面代码中直接调用方法就可以成功了。
【问题讨论】: