javascript 如何以编程方式在 CKEDITOR 中选择文本范围?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4401469/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 11:17:57  来源:igfitidea点击:

how to select a text range in CKEDITOR programatically?

javascriptseleniumckeditorfckeditor

提问by Gabriel Falc?o

Problem:

问题:

I have a CKEditor instance in my javascript:

我的 javascript 中有一个 CKEditor 实例:

var editor = CKEDITOR.instances["id_corpo"];

and I need to insert some text programatically, and select some text range afterwards.

我需要以编程方式插入一些文本,然后选择一些文本范围。

I already did insert text through

我已经通过插入文本

editor.insertHtml('<h1 id="myheader">This is a foobar header</h1>');

But I need to select (highlight) the word "foobar", programatically through javascript, so that I can use selenium to work out some functional tests with my CKEditor plugins.

但是我需要通过 javascript 以编程方式选择(突出显示)“foobar”这个词,以便我可以使用 selenium 用我的 CKEditor 插件进行一些功能测试。

UPDATE 1:

更新 1:

I've also tried something like

我也试过类似的东西

var selection = editor.getSelection();
var childs = editor.document.getElementsByTag("p");
selection.selectElement(childs);

But doesn't work at all!

但根本不起作用!

How can I do that?

我怎样才能做到这一点?

I think that

我觉得

selection.selectRange()

could do the job, but I'could not figure out how to use it. There are no examples over there :(

可以完成这项工作,但我不知道如何使用它。那边没有例子:(

回答by Siva

Get current selection

获取当前选择

var editor = CKEDITOR.instances["id_corpo"];
var sel = editor.getSelection();

Change the selection to the current element

将选择更改为当前元素

var element = sel.getStartElement();
sel.selectElement(element);

Move the range to the text you would like to select

将范围移动到您要选择的文本

var findString = 'foobar';
var ranges = editor.getSelection().getRanges();
var startIndex = element.getHtml().indexOf(findString);
if (startIndex != -1) {
    ranges[0].setStart(element.getFirst(), startIndex);
    ranges[0].setEnd(element.getFirst(), startIndex + findString.length);
    sel.selectRanges([ranges[0]]);
}

回答by Jajo

You can also do the following:

您还可以执行以下操作:

get the current selection

获取当前选择

var selection = editor.getSelection();
var selectedElement = selection.getSelectedElement();

if nothing is selected then create a new paragraph element

如果未选择任何内容,则创建一个新的段落元素

if (!selectedElement)
    selectedElement = new CKEDITOR.dom.element('p');

Insert your content into the element

将您的内容插入元素

selectedElement.setHtml(someHtml);

If needed, insert your element into the DOM (it will be inserted into the current position)

如果需要,将您的元素插入 DOM(它将被插入到当前位置)

editor.insertElement(selectedElement);

and then just select it

然后只需选择它

selection.selectElement(selectedElement);

回答by simshaun

Check out the selectElement() method of CKEDITOR.dom.selection.

查看 CKEDITOR.dom.selection 的 selectElement() 方法。

http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.dom.selection.html

http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.dom.selection.html

回答by Nidhi

insert text at cursor point in ck editor

在 ck 编辑器的光标点插入文本

  • function insertVar(myValue) { CKEDITOR.instances['editor1'].fire( 'insertText',myValue); }

    this is working for me

  • 函数 insertVar(myValue) { CKEDITOR.instances['editor1'].fire('insertText',myValue); }

    这对我有用