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

javascript - How can I trigger an onkeydown event on html table on Firefox? - Stack Overflow

programmeradmin3浏览0评论

I want to capture the keystrokes on a table's cells. I have the following code that is working on IE but not on Firefox/Chrome.

<table id="testing" onkeydown="alert('testing')"><br />
    <tr><td>testing</td></tr>` <br />
    </table>
<br />

Any suggestion?

I want to capture the keystrokes on a table's cells. I have the following code that is working on IE but not on Firefox/Chrome.

<table id="testing" onkeydown="alert('testing')"><br />
    <tr><td>testing</td></tr>` <br />
    </table>
<br />

Any suggestion?

Share Improve this question edited Feb 27, 2017 at 3:32 Paul Rooney 21.6k9 gold badges44 silver badges62 bronze badges asked May 20, 2009 at 12:10 StavrosStavros 6,13013 gold badges33 silver badges45 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 11

http://www.w3schools./jsref/jsref_onkeydown.asp specifies that most of the tags support onkeydown.

You need to set tabindex property (eg tabindex="1") to some value, so it can identify that this table is keyboard selectable. That allows the keyboard event to be triggered. Following will work in firefox:

<table id="testing" onkeydown="alert('testing')" tabindex="0"><br />
<tr><td>testing</td></tr><br />
</table> 

This appears to be an issue with Firefox and Chrome's interpretation of the specifications rather than your code. I just tried this myself and reproduced the issue.

The W3C specifications show that Table cells should support onkeydown but this doesn't appear to be supported by Firefox/Chrome currently, you could try reporting this as a Firefox bug through their website. You usually get some feedback as to why it's broken (or why they think it's not broken and won't fix it as I've had in the past)

Can I ask why you need to capture keystrokes on table cells?

  • If you are expecting people to enter text then you should use <input> tags which will register key press events fine.
  • If the keystrokes are some kind of navigation then perhaps a context menu done based on mouse click events would be better
发布评论

评论列表(0)

  1. 暂无评论