Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/471.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Can';当contenteditable=true时,捕获javascript/jQuery中的第一个onkeypress事件_Javascript_Jquery_Javascript Events - Fatal编程技术网

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>