问题
CK editor find and replace will work only first time then it always gives index error in ranges[0].setStart
function.
I have tried with editor.updateElement();
but it's not working.
function test() {
try {
var editor = CKEDITOR.instances[("<%=ckDescription.ClientID %>")];
var sel = editor.getSelection();
var element = sel.getStartElement();
sel.selectElement(element);
var findString = 'FE';
var ranges = editor.getSelection().getRanges();
var startIndex = element.getText().indexOf(findString);
if (startIndex != -1) {
ranges[0].setStart(element.getFirst(), startIndex);
ranges[0].setEnd(element.getFirst(), startIndex + findString.length);
sel.selectRanges([ranges[0]]);
}
var range = sel.getRanges()[0];
range.deleteContents();
range.select();
editor.insertText('For Example');
editor.updateElement();
}
catch (e) {
alert(e);
return false;
}
return false;
}
Error :
IndexSizeError: Failed to execute 'setStart' on 'Range': The offset 36 is larger than or equal to the node's length (17).
Thanks.
回答1:
Each and every time before creating ckeditor instance you should try this CKEDITOR.instances={}
Sometime CKEDITOR.instance object use to hold the previous instances as object property, so next time find and replace function stop working because of the previous instance with the same named property. So the only solution is to destroy the previous instance. One way is to call
editor.destroy() / CKEDITOR.instances.("<%=ckDescription.ClientID %>").destroy()
来源:https://stackoverflow.com/questions/29910435/ck-editor-find-and-replace-will-works-only-first-time-then-it-will-gives-index-e