admin管理员组文章数量:1387319
jsFiddle demo link
Hi Guys, I want to bind the keypress event handler when i click the div layer as given in the above demo. The feature is to use the delete key to remove the selected layer.
But when i hit the delete key, the keypress handler doesn't fire. Please suggest.
jsFiddle demo link
Hi Guys, I want to bind the keypress event handler when i click the div layer as given in the above demo. The feature is to use the delete key to remove the selected layer.
But when i hit the delete key, the keypress handler doesn't fire. Please suggest.
Share Improve this question edited Jun 14, 2012 at 6:31 Engineer 48.8k12 gold badges90 silver badges92 bronze badges asked Jun 13, 2012 at 6:45 KaranKaran 3,33810 gold badges61 silver badges88 bronze badges3 Answers
Reset to default 4Remove '()
':
$this.on("keypress", keyAction);
You are adding undefined
as handler: $this.on("keypress", keyAction())
is equal to $this.on("keypress", undefined)
in your case, as keyAction
function does not return anything.
Also your div
must be focusable, in order to receive keyboard events. For that reason you need to add tabindex
on it:
<div class="dragClass" tabindex="0">
Then in selectAction
focus your div to receive keypress event:
$this.focus();
This is the DEMO
For more information about adding keyboard event
s on static elements such as div
, look at here:
Use this code it execute for delete key
$("#ParentDIV").keyDown(function (e) {
if (e.keyCode == 46) {
// Do it
}
});
In order for an element to receive keypress events, it needs focus. One way is to add a tabindex. If you click on your element then click a keyboard button in the below fiddle, you'll get your event. I stripped down the fiddle a bit and removed the draggable which is interfering with focus.
http://jsfiddle/e2yfC/27/
本文标签: javascriptJquery UIdynamically bind the keypress event on the clicked divStack Overflow
版权声明:本文标题:javascript - Jquery UI - dynamically bind the keypress event on the clicked div - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744508497a2609732.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论