点击画布元素内的文字 [英] Clicking inside canvas element selects text

查看:72
本文介绍了点击画布元素内的文字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的HTML文档中有一个画布元素。当我多次点击画布内部时,它会选择< canvas> << c $ c< / code>标签。有没有办法阻止这种情况发生?我猜测有一个JavaScript解决方案。

解决方案

返回 false in一个事件停止标准事件的发生:

pre $ document.getElementById('canvas')。onmousedown = function(){
返回false;
};

编辑:我发现文本选择是在 onclick code>被触发,更好的选择是 onmousedown

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.

解决方案

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.

这篇关于点击画布元素内的文字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆