Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
503 views
in Technique[技术] by (71.8m points)

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Give the canvas a tab index:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...