How to add event listener to div
2016-06-25 01:36
489 查看
http://stackoverflow.com/questions/148361/how-can-i-give-keyboard-focus-to-a-div-and-attach-keyboard-event-handlers-to-it
I am building an application where I want to be able to click a rectangle represented by a DIV, and then use the keyboard to move that DIV
Rather than using an event listener for keyboard events at the document level, can I listen for keyboard events at the DIV level, perhaps by giving it keyboard focus?
Here's a simplified sample to illustrate the problem:
On clicking the inner DIV I try to give it focus, but subsequent keyboard events are always picked up at the document level, not my DIV level event listener.
Do I simply need to implement an application-specific notion of keyboard focus?
I should add I only need this to work in Firefox.
Answer:
Sorted - I added tabindex attribute to the target DIV, which causes it to pick up keyboard events, for example
Information gleaned from http://www.w3.org/WAI/GL/WCAG20/WD-WCAG20-TECHS/SCR29.html
I am building an application where I want to be able to click a rectangle represented by a DIV, and then use the keyboard to move that DIV
Rather than using an event listener for keyboard events at the document level, can I listen for keyboard events at the DIV level, perhaps by giving it keyboard focus?
Here's a simplified sample to illustrate the problem:
<html> <head> </head> <body> <div id="outer" style="background-color:#eeeeee;padding:10px"> outer <div id="inner" style="background-color:#bbbbbb;width:50%;margin:10px;padding:10px;"> want to be able to focus this element and pick up keypresses </div> </div> <script language="Javascript"> function onClick() { document.getElementById('inner').innerHTML="clicked"; document.getElementById('inner').focus(); } //this handler is never called function onKeypressDiv() { document.getElementById('inner').innerHTML="keypress on div"; } function onKeypressDoc() { document.getElementById('inner').innerHTML="keypress on doc"; } //install event handlers document.getElementById('inner').addEventListener("click", onClick, false); document.getElementById('inner').addEventListener("keypress", onKeypressDiv, false); document.addEventListener("keypress", onKeypressDoc, false); </script> </body> </html>
On clicking the inner DIV I try to give it focus, but subsequent keyboard events are always picked up at the document level, not my DIV level event listener.
Do I simply need to implement an application-specific notion of keyboard focus?
I should add I only need this to work in Firefox.
Answer:
Sorted - I added tabindex attribute to the target DIV, which causes it to pick up keyboard events, for example
<div id="inner" tabindex="0"> this div can now have focus and recieve keyboard events </div>
Information gleaned from http://www.w3.org/WAI/GL/WCAG20/WD-WCAG20-TECHS/SCR29.html
相关文章推荐
- Scala "Hello,world!" 程序
- MySQL子查询(subquery)分类
- 学期总结
- 关于创建一个对象的流程以及代码块和属性位置相关
- 34. 后台模块开发(1)
- 站在互联网上,看宋江如何在创业公司当老大
- form表单那点事儿(上) 基础篇
- maven配置plugin报错vc-complex-type.2.4.a: Invalid content was found starting with element 'plugins'
- node入门
- 算法学习笔记(一)——算法概述
- Oracle进程管理常用查询语句
- 理解devfs、sysfs、udev
- sublime个性化设置
- linux内核sysfs详解
- [Webpack 2] Import a non-ES6 module with Webpack
- hadoop安装后测试运行
- "AsynchTask和Handler"的区别与用法-Android异步消息处理机制之图文代码详解
- JAVA 微信JSSDK Config参数配置方法
- 使用Maven插件Cargo实现自动化部署
- TabLayout+ViewPager+Fragment实现带图标和文字的底部导航栏