【问题标题】:How to place an html element at the cursor position in Ckeditor?如何在 Ckeditor 的光标位置放置一个 html 元素?
【发布时间】:2013-05-25 19:09:04
【问题描述】:

我很难成功地将我拖放到我的 Ckeditor 中的 html 元素。 到目前为止,我刚刚能够使用“setData”将它放在我的内容的最后。 但我想把它放在光标所在的位置。

我的意思是,不要这样做:

<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>');

我已经尝试过 insertText,但它从来没有奏效。 然后我尝试了 insertHtml 但它只在 IE o_O 中有效。

您知道如何解决它吗? 那将是一个很大的帮助! 谢谢。

【问题讨论】:

  • 你确定编辑在你打电话给insertHtml时有选择吗?我确信insertTextinsertHtml 两种方法在正确使用时都有效。检查此示例:ckeditor.com/latest/samples/api.html - “插入 HTML”按钮 WFM。

标签: javascript cursor position ckeditor inserthtml


【解决方案1】:

使用 Ckeditor '粘贴'(删除)事件/方法而不是原生。

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

粘贴和删除由同一个事件“粘贴”处理。 ckeditor 将 dataValue 的内容放在编辑器的光标位置。

editor.on('paste', function(e){
    // get data from e.data.dataTransfer or wherever ...
    e.data.dataValue = ' <span>The html I am drag/droping</span>';
});
猜你喜欢
  • 2013-07-08
  • 2015-03-13
  • 1970-01-01
  • 1970-01-01
  • 2023-03-10
  • 1970-01-01
  • 1970-01-01
  • 2011-05-31
  • 2022-06-15
相关资源
最近更新 更多