jQuery中的off()没有删除鼠标监听器

时间:2013-09-27 12:18:33

标签: jquery html5-canvas

我使用.on()将一些鼠标事件绑定到html5画布。要取消绑定这些事件,请使用.off()。但是这些功能仍然被解雇了。我正在使用jQuery 1.9.2。

$('body').on('mousedown',$('#drawingCanvas'),function (e) { 
  handleMouseDown(e);
});
$('body').on('mousemove',$('#drawingCanvas'),function (e) {  
   handleMouseMove(e);
});
$('body').on('mouseup',$('#drawingCanvas'),function (e) {
    handleMouseUp(e);
});
$('body').on('mouseout',$('#drawingCanvas'),function (e) {      
    handleMouseOut(e);
    isMouseDown = false;
});

function handleMouseMove(e) {
    mouseX = parseInt(e.clientX - offsetX);
    mouseY = parseInt(e.clientY - offsetY); 
    if (isMouseDown) {
        context.beginPath();
        context.globalCompositeOperation = "source-over";
        context.moveTo(lastX, lastY);
        context.lineTo(mouseX, mouseY);
        context.stroke();
        lastX = mouseX;
        lastY = mouseY;
    }
}

$('body').off('mousedown',$('#drawingCanvas'));
$('body').off('mousemove',$('#drawingCanvas'));
$('body').off('mouseup',$('#drawingCanvas'));   
$('body').off('mouseout',$('#drawingCanvas'));

1 个答案:

答案 0 :(得分:0)

$('body').on('mousedown','#drawingCanvas',handleMouseDown);

$('body').off('mousedown',handleMouseDown);