CK editor find and replace will works only first time then it will gives index error in ranges[0].setStart function

为君一笑 提交于 2019-12-23 02:39:33

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!