Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/391.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
用纯javascript聚焦出一个可编辑的p标记_Javascript_Html_Dom - Fatal编程技术网

用纯javascript聚焦出一个可编辑的p标记

用纯javascript聚焦出一个可编辑的p标记,javascript,html,dom,Javascript,Html,Dom,我有以下html <p contenteditable>The greatest p tag of all p tags all the time</p> 但这东西不行。如何使用enter hit聚焦p标记。请不要用jquery。谢谢:-)我认为正确的方法是.blur() 因此,以下几点就足够了: var p = document.querySelector('p'); p.addEventListener('keypress', function (e) { //

我有以下html

<p contenteditable>The greatest p tag of all p tags all the time</p>


但这东西不行。如何使用enter hit聚焦p标记。请不要用jquery。谢谢:-)

我认为正确的方法是
.blur()

因此,以下几点就足够了:

var p = document.querySelector('p');

p.addEventListener('keypress', function (e) { // you spelt addEventListener wrongly
    if (e.which === 13) {
        e.preventDefault();// to prevent the default enter functionality
        this.blur();            
    }
});

尝试使用
event.preventDefault()
,创建
input
元素,该元素具有
宽度
高度
设置为
0px
不透明度
设置为
0
。如果
event.keyCode
等于
13
,则在
输入上调用
.focus()
,使用
不透明度
0
var p=document.querySelector('p');
p、 onkeypress=功能(e){
如果(如keyCode===13){
e、 预防默认值();
document.getElementById(“focus”).focus();
}
};
#焦点{
宽度:0;
身高:0;
不透明度:0;
}

所有p标签中一直最大的p标签


您可以使用
onblur
onfocusout
事件:

<p onblur="myFunction()" contenteditable>
    The greatest p tag of all p tags all the time
</p>

一直以来所有p标签中最大的p标签

一直以来所有p标签中最大的p标签


您拼错了
Listener
,因此按键没有被抓到。您还需要防止像这样的按键

var p=document.querySelector('p');
p、 addEventListener(“按键”,功能(e){
如果(e.which==13){
这个。blur();
e、 预防默认值();
}
});

所有p标记中最伟大的p标记

您刚刚错过了
e.preventDefault()更正了
<p onblur="myFunction()" contenteditable>
    The greatest p tag of all p tags all the time
</p>
<p onfocusout="myFunction()" contenteditable>
    The greatest p tag of all p tags all the time
</p>