【发布时间】:2017-07-25 10:33:55
【问题描述】:
我有一个带有点击绑定的类选择器。当我点击不止一次时会出现问题,它在第一次调用时调用了两次,在下一次点击时调用了三次,依此类推。
var point = data.points[0].x;
var counter = 0;
$(".menuOption").click(function () {
console.log(counter);
counter++;
var code = $(this).attr('id');
var text = $(this).html();
var newLine = {
type: 'line',
x0: point,
x1: point,
y0: 0,
y1: 1,
yref: 'paper',
line: {
color: 'black',
width: 1
},
name: text
};
var annotation = {
x: point,
y: data.points[0].y,
xref: 'x',
yref: 'y',
text: code,
textangle: 90,
showarrow: true,
arrowhead: 7
}
Plotly.relayout("grap", {
'shapes[0]': newLine,
'hovermode': 'closest',
'annotations[0]': annotation
});
});
我在下面的笔中添加了一些console.log。
【问题讨论】:
-
把 unbind 放在前面,$(".menuOption").unbind();
-
它工作了一点......它仍然重复该功能。我正在使用情节处理程序,也许它有一些差异。其实我不知道。
标签: javascript jquery class selector