简体   繁体   English

将原子内容插入CKEditor

[英]Insert atomic content into CKEditor

Is it possible to insert "atomic" block content into CKEditor? 是否可以在CKEditor中插入“原子”块内容?

For example I want to insert <h1>Test</h1> just after "B" in <p>AB C</p> . 例如,我想在<p>AB C</p> “ B”之后插入<h1>Test</h1> <p>AB C</p> Currently with CKEDITOR.currentInstance.insertHtml('<h1>Test</h1>') , the block tag is removed and it becomes <p>A BTest C</p> (cursor it just after "B"). 当前使用CKEDITOR.currentInstance.insertHtml('<h1>Test</h1>') ,该块标记已被删除,并且变为<p>A BTest C</p> (光标<p>A BTest C</p>在“ B”之后)。 I want it to be inserted at the end of the P block where the cursor is, like <p>AB C</p><h1>Test</h1> 我希望将其插入到光标所在的P块的末尾,例如<p>AB C</p><h1>Test</h1>

Here is a working example http://jsfiddle.net/T49Pf/3/ . 这是一个工作示例http://jsfiddle.net/T49Pf/3/ When the caret is anywhere in the first paragraph and I click "Insert", I don't have the h1 tag inserted. 当插入符号在第一段中的任意位置,并且我单击“插入”时,我没有插入h1标签。 But when the caret is in the second (empty) paragraph, I do have the h1 block element. 但是,当插入符号位于第二(空)段中时,我确实具有h1块元素。 Now I'd like when I click "Insert" anywhere, the content is inserted in a place that keep it atomic (don't lost the h1 tag). 现在,我想在任何地方单击“插入”时,将内容插入保持原子性的位置(不要丢失h1标签)。 It is similar to the "magic line" plugin. 它类似于“魔术线”插件。

I assume that you're using editor.insertHtml method. 我假设您正在使用editor.insertHtml方法。 This method's behaviour was designed to work with pasted code and your case is most often means that single line or part of it was copied so it should be handled as a text - not as a format. 此方法的行为旨在与粘贴的代码一起使用,您的案例通常是指复制了一行或一部分代码,因此应将其作为文本而不是格式来处理。

In your case I'd recommend the editor.insertElement method which is for different purposes (like eg image insertion) so it will preserve your element. 在您的情况下,我建议您使用editor.insertElement方法,该方法可用于不同的目的(例如插入图像),以便保留您的元素。

Sample from documentation: 文档样本:

var element = CKEDITOR.dom.element.createFromHtml( '<img src="hello.png" border="0" title="Hello" />' );
CKEDITOR.instances.editor1.insertElement( element );

标头标签(h1-6)是块级元素,不能嵌套在<p>标签内...

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

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