粘贴< / p>关闭标签而不会在contenteditable中产生开放p标签 [英] Paste </p> closing tag without spawning an opening p tag in contenteditable
问题描述
我有一个可以理解的div:
< div id =resultclass =contentcontenteditable = 真 >< p为H.;<峰; br />< / p>< / DIV>
我想要新的段落形成,当我按下ENTER时,为了截取ENTER keydown并替换插入一个html代码的默认动作:
$ $ $ $content)。on(keydown,function( e){if(e.which == 13){e.preventDefault(); pasteHtmlAtCaret(< / p>< p>< br />);}});
我希望当我按下ENTER时,< / p> < p>< br />
会关闭现有的段落并开启新的段落。所以,如果我输入:'hello world'+ ENTER,我应该:
< p> hello world< / p> ;< p为H.;<峰; br />< / p为H.
您可以识别< / p>< p>< ; br />
一块。
我观察到了不同的行为。我得到:
< p> hello world< p>< / p>< p>< br><< ; / p>< / p为H.
因此,看起来第一个< / p>
催生了一个开头< p>
标记,而< p>
就在< br />
标签催生了一个匹配的结束p标签。这是否意味着我的浏览器忽略了包装< p>< / p>
的存在?
当我插入我的一小段HTML代码时,如何避免产生新的 p
标签?
我使用Chromium和jQuery 1.7.2。
编辑发生在文档对象模型(DOM)中。 HTML标签在编辑器中并不存在。 HTML被解析成一个DOM。当DOM被序列化时,HTML标签再次被生成。因此,不正确的HTML片段无法被粘贴而无任何后果。
I've got a contenteditable div:
<div id="result" class="content" contenteditable="true"><p><br/></p></div>
I want new paragraphs to form when I press ENTER, and for that I intercept ENTER keydown and replace the default action by the insertion of a html code:
$(".content").on("keydown",function(e){if(e.which == 13) { e.preventDefault(); pasteHtmlAtCaret("</p><p><br/>");}});
My hope was that when I press ENTER, the </p><p><br/>
would close the existing paragraph and open a new paragraph. So, if I typed: 'hello world' + ENTER, I should have:
<p>hello world</p><p><br/></p>
where you can recognize the </p><p><br/>
piece.
I observe a different behaviour though. I get:
<p>hello world<p></p><p><br></p></p>
So it seems that the first</p>
has spawned an opening <p>
tag and that the <p>
just before the <br/>
tag has spawned a matching closing p tag. Does that mean that my browser ignores the existence of a wrapping <p></p>
?
How can I avoid the spawning a new p
tags when I insert my little piece of html code?
I'm using Chromium and jQuery 1.7.2.
Editing occurs in a document object model (DOM). The HTML tags don't really exist in the editor. The HTML is parsed into a DOM. The HTML tags are produced again when the DOM is serialized. Therefore, improper HTML fragments cannot be pasted without consequences.
这篇关于粘贴< / p>关闭标签而不会在contenteditable中产生开放p标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!