Can';当contenteditable=true时,捕获javascript/jQuery中的第一个onkeypress事件
代码如下:Can';当contenteditable=true时,捕获javascript/jQuery中的第一个onkeypress事件,javascript,jquery,javascript-events,Javascript,Jquery,Javascript Events,代码如下: <div onclick="this.setAttribute('contenteditable', 'true');" onkeypress="console.info(event.keyCode)">foo</div> foo 这就是我期望的工作方式:用户单击一个div,它是可编辑的,然后我可能会截获一些关键事件,以使用Enter键、Tab键和箭头键运行自定义处理 问题是,可以在单击后编辑div,但是不能从onkeypress获取第一个按键事件
<div onclick="this.setAttribute('contenteditable', 'true');"
onkeypress="console.info(event.keyCode)">foo</div>
foo
这就是我期望的工作方式:用户单击一个div
,它是可编辑的,然后我可能会截获一些关键事件,以使用Enter键、Tab键和箭头键运行自定义处理
问题是,可以在单击后编辑div
,但是不能从onkeypress
获取第一个按键事件。只有按两次才能获得按键事件
我在Win32位上使用ChromeDev12。请帮助您可以这样做(jquery方式):
foo
$(文档).ready(函数(){
$(“#div1”)。单击(函数(){
$(this.attr(“contenteditable”,true);
}).按键(功能(事件){
控制台信息(事件键码);
});
});
希望这有帮助。干杯尝试添加
此项。重点如下:
<div onclick="this.setAttribute('contenteditable', 'true');this.focus();"
onkeypress="console.info(event.keyCode)">foo</div>
foo
我在这里试过,效果很好。Ha,这正是我问的原因。它不会工作(至少在Chrome上)
<div onclick="this.setAttribute('contenteditable', 'true');this.focus();"
onkeypress="console.info(event.keyCode)">foo</div>