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

javascript - How do you set focus to the HTML5 canvas element? - Stack Overflow

programmeradmin1浏览0评论

I'm using the HTML5 <canvas> element in Firefox 2.0.0.16 and in Safari 3.1.2, both on my iMac. (I've tried this in Firefox 3.0 on Windows as well, also to no avail.) The tag looks like this:

<td>
   <canvas id="display"
           width="500px"
           height="500px">
   </canvas>
</td>

I have a button to "activate" some functionality that interacts with the canvas. That button's onclick() event calls a function. In that function I have the following line:

document.getElementById("display").focus();

This does not work. Firebug reports no error. But the focus still remains where it was. I can click on the canvas or tab towards the canvas and focus will be lost from the other elements, but apparently never be gained on by the canvas (The canvas's onfocus() event never fires).

I find this odd. Is it that the canvas simply cannot get focus, or am I missing something here? Any insight would be appreciated.

Thank you.

I'm using the HTML5 <canvas> element in Firefox 2.0.0.16 and in Safari 3.1.2, both on my iMac. (I've tried this in Firefox 3.0 on Windows as well, also to no avail.) The tag looks like this:

<td>
   <canvas id="display"
           width="500px"
           height="500px">
   </canvas>
</td>

I have a button to "activate" some functionality that interacts with the canvas. That button's onclick() event calls a function. In that function I have the following line:

document.getElementById("display").focus();

This does not work. Firebug reports no error. But the focus still remains where it was. I can click on the canvas or tab towards the canvas and focus will be lost from the other elements, but apparently never be gained on by the canvas (The canvas's onfocus() event never fires).

I find this odd. Is it that the canvas simply cannot get focus, or am I missing something here? Any insight would be appreciated.

Thank you.

Share Improve this question edited May 14, 2013 at 12:26 Tiago Sippert 1,3327 gold badges24 silver badges33 bronze badges asked Sep 11, 2008 at 14:55 AlanAlan 3,8951 gold badge30 silver badges35 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 30

Give the canvas a tab index:

   <canvas id="display"
           width="500px"
           height="500px"
           tabindex="1">
   </canvas>
发布评论

评论列表(0)

  1. 暂无评论