Clicking inside canvas element selects text

后端 未结 2 1736
太阳男子
太阳男子 2020-12-20 20:11

I have a canvas element in my HTML document. When I click inside of the canvas multiple times, it selects part of my

element\'s text, which is before

相关标签:
2条回答
  • 2020-12-20 20:51

    Returning false in an event stops the standard event from happening:

    document.getElementById('canvas').onmousedown = function(){
      return false;
    };
    

    Edit: I just found out that text selection is done before onclick is fired, a better option is onmousedown.

    0 讨论(0)
  • 2020-12-20 20:52

    If you want to use the mousedown event to do other things, you can prevent only text selection more specifically by setting the onselectstart event to return false.

    //give your canvas an id, I used 'can'    
    var canvas = document.getElementById('can');
    canvas.onselectstart = function () { return false; }
    
    0 讨论(0)
提交回复
热议问题