javascript - AddActionListener 在初始化而不是点击事件时激活
问题描述
我正在编写一个在线图表编辑工具,并创建了一个函数来设置我在 init 函数中调用的事件侦听器。我尝试添加许多断点以查看问题出在哪里,似乎 addactionlistener 函数调用了参数中的函数而没有发生动作事件。
init(){
console.log("Website started");
myObj = new Draw();
setUpEventListener();
}
gates = {
or_gate: '#or_gate',
and_gate: '#and_gate',
not_gate: '#not_gate'
}
shapes = {
or_gate: '#or_gate_shape',
and_gate: '#and_gate_shape',
not_gate: '#not_gate_shape'
}
setUpEventListener(){
document.querySelector('#OrCloneBtn').addEventListener('click', this.ctrlGateSVG(this.gates.or_gate, this.shapes.or_gate));
document.querySelector('#AndCloneBtn').addEventListener('click', this.ctrlGateSVG(this.gates.and_gate, this.shapes.and_gate));
document.querySelector('#NotCloneBtn').addEventListener('click', console.log("mathieu"));
}
basicAttr = {
"fill" : "#FFF",
"stroke" : "black",
"strokeWidth" : 1
}
ctrlGateSVG(gate, shape){
var to = Snap('#gates');
var from = Snap(gate);
var shape = from.select(shape);
var clone = shape.clone();
clone.attr(this.basicAttr);
to.append(clone);
console.log("clicked")
}
drawCanvas() {
var lines = Snap("#lineBackground");
var height = document.querySelector('.drawPart').clientHeight;
var width = document.querySelector('.drawPart').clientWidth;
for(var i=0; i < height; i = i+2){
lines.line(i, 0, i, height).attr({
stroke:'black',
strokeWidth:.2,
});
}
for(i=0; i<width; i=i+2){
lines.line(0, i, width, i).attr({
stroke:'black',
strokeWidth:.2
});
}
}
init(){
console.log("Website started");
var myObj = new Draw();
this.drawCanvas();
this.setUpEventListener();
}
}
myApp = new DrawingCanvas();
myApp.init();
请你能解释一下这里有什么问题,因为我真的很困惑。
解决方案
推荐阅读
- php - Laravel 具有多个集合的多个过滤器表
- android - 如果在获取 API 期间按下后退按钮,Kotlin 应用程序崩溃
- c++ - 使用 C++ 查找 lapindromes 时出现运行时错误“SIGCONT”
- python - X-Tick 标签不会旋转![如何使用子图和图形在 Seaborn 中旋转 X-Tick 标签]
- python - 我的 pre_save 信号不起作用,因为我想使用此信号更新我的 slug 字段
- cs50 - CS50 pset4 滤镜反射
- mysql - 从sql中的表中获取最后输入的id
- javascript - 通过javascript大量下载图像在IOS中不起作用
- python - 使用带有条件和 NaN 值的 map 函数时出现问题
- elasticsearch - 当 Elasticsearch 中有多个版本时,Update Builder 会延迟响应?