简体   繁体   English

document.execCommand-insertParagraph无法正常工作

[英]document.execCommand - insertParagraph isn't working

I'm trying to toggle the application of <p>...</p> tag around the selection in a contentEditable, for which I'm using 我正在尝试在contentEditable的选择范围内切换<p>...</p>标记的应用

document.execCommand('insertParagraph', false, null);

Forget about toggling the p-tag, I'm not able to apply the tag to the selection. 忘记切换p标签,我无法将标签应用于选择。 Instead, it is giving 相反,它给

<div>
    <br>
</div>
<div>
    <br>
</div>

This is happening with all the browsers (chrome, firefox, IE). 所有浏览器(chrome,firefox,IE)都在发生这种情况。 It is removing the selection and creating the above empty divs. 它正在删除选择并创建上面的空div。

I know its bit late but i found the solution 我知道有点晚了,但是我找到了解决方案

document.execCommand('formatblock',false,'p');

It worked for me in both chrome and firefox. 它适用于Chrome和Firefox。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM