最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Clicking inside canvas element selects text - Stack Overflow

programmeradmin3浏览0评论

I have a canvas element in my HTML document. When I click inside of the canvas multiple times, it selects part of my <h1> element's text, which is before the <canvas> tag. Is there a way to stop this from happening? I am guessing there is a JavaScript solution.

I have a canvas element in my HTML document. When I click inside of the canvas multiple times, it selects part of my <h1> element's text, which is before the <canvas> tag. Is there a way to stop this from happening? I am guessing there is a JavaScript solution.

Share Improve this question asked Sep 26, 2010 at 20:29 user142162user142162
Add a ment  | 

2 Answers 2

Reset to default 15

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.

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)

  1. 暂无评论