简体   繁体   English

CKEditor:调用编辑器#setData后恢复插入位置

[英]CKEditor: Restore caret position after calling editor#setData

I have a CKEditor instance where I want to manipulate the content, and restore the caret position to where it was afterwards. 我有一个CKEditor实例,我想操作内容,并将插入位置恢复到之后的位置。 The problem is that, when you call setData , it resets the caret to the beginning of the editor. 问题是,当您调用setData ,它会将插入符重置为编辑器的开头。 This is understandable if you are changing all of the content, but I am only making minor changes to the data. 如果要更改所有内容,这是可以理解的,但我只是对数据进行了少量更改。

editor.on('change', function () {
  var data = editor.getData();
  // manipulate `data`
  var manipulatedData = data;
  editor.setData(manipulatedData);
});

I found a simple solution for you query. 我找到了一个简单的解决方案供您查询。 instead of adding setdata. 而不是添加setdata。 you can use inserthtml 你可以使用inserthtml

editor.insertHtml(manipulatedData).

Will keep the cursor at the end position after inserting the data 插入数据后,将光标保持在结束位置

A simple solution is setData to '', then use insertHtml with your content. 一个简单的解决方案是将setData设置为'',然后将insertHtml与您的内容一起使用。 setData is asyncronius, so you must use a callback function. setData是asyncronius,因此您必须使用回调函数。 This is the code that works: 这是有效的代码:

oEditor.setData('', {callback: function() {
oEditor.insertHtml(YOUR_HTML);
}
});

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

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