如何将html元素放置在Ckeditor中的光标位置

How to place an html element at the cursor position in Ckeditor?

本文关键字:Ckeditor 光标 位置 html 元素      更新时间:2023-09-26

我有这样的困难,成功地放置一个html元素,我拖放到我的Ckeditor。到目前为止,我只能用setData&quot把它放在我的内容的最后。但是我想把它放在光标的位置

我的意思是,与其这样做:

<p>My content with <span>spans</span>, <a>links</a>, etc.</p><span>The html I am drag/droping</span>

我想这样做:

<p>My content with <span>spans</span>, <span>The html I am drag/droping</span>, <a>links</a>, etc.</p>

现在,我的代码看起来像这样:

CKEDITOR.instances['myContent'].insertHtml(' <span>The html I am drag/droping</span>');
  • 我试过插入文本,但它从来没有工作。
  • 然后我尝试了inserthhtml,但它只在IE中工作。

你知道怎么修理它吗?那可帮了大忙了!谢谢。

使用Ckeditor 'paste' (drop) events/methods代替native。

在你的插件editor.on('instanceReady'…假设CKEditor 4.x

粘贴和删除由同一个事件' Paste '处理。Ckeditor将datvalue的内容放在编辑器的光标位置。

editor.on('paste', function(e){
    // get data from e.data.dataTransfer or wherever ...
    e.data.dataValue = ' <span>The html I am drag/droping</span>';
});