Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/406.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何以编程方式在CKEDITOR中选择文本范围? 问题:_Javascript_Selenium_Ckeditor_Fckeditor - Fatal编程技术网

Javascript 如何以编程方式在CKEDITOR中选择文本范围? 问题:

Javascript 如何以编程方式在CKEDITOR中选择文本范围? 问题:,javascript,selenium,ckeditor,fckeditor,Javascript,Selenium,Ckeditor,Fckeditor,我的javascript中有一个CKEditor实例: var editor = CKEDITOR.instances["id_corpo"]; 我需要通过编程插入一些文本,然后选择一些文本范围 我已经插入了文本 editor.insertHtml('<h1 id="myheader">This is a foobar header</h1>'); 但根本不起作用! 我该怎么做? 我认为 selection.selectRange() 我能做这项工作,但我不知道如何

我的javascript中有一个CKEditor实例:

var editor = CKEDITOR.instances["id_corpo"];
我需要通过编程插入一些文本,然后选择一些文本范围

我已经插入了文本

editor.insertHtml('<h1 id="myheader">This is a foobar header</h1>');
但根本不起作用! 我该怎么做? 我认为

selection.selectRange()
我能做这项工作,但我不知道如何使用它。
这里没有示例:(

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


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

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

获取当前选择

var editor = CKEDITOR.instances["id_corpo"];
var sel = editor.getSelection();
var selection = editor.getSelection();
var selectedElement = selection.getSelectedElement();
将所选内容更改为当前元素

var element = sel.getStartElement();
sel.selectElement(element);
将范围移动到要选择的文本

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]]);
}

您还可以执行以下操作:

获取当前选择

var editor = CKEDITOR.instances["id_corpo"];
var sel = editor.getSelection();
var selection = editor.getSelection();
var selectedElement = selection.getSelectedElement();
如果未选择任何内容,则创建一个新段落元素

if (!selectedElement)
    selectedElement = new CKEDITOR.dom.element('p');
将内容插入元素中

selectedElement.setHtml(someHtml);
如果需要,将元素插入DOM(它将插入当前位置)

然后选择它

selection.selectElement(selectedElement);

我已经尝试了selectElement,但没有成功GetElementsByTag返回元素集合。请仔细检查您正在使用的对象、它们的方法和属性以及是否存在任何错误。如果不清楚某个方法的作用或失败原因,您可以使用CKEditor源文件调试调用。我尝试了当选择单个标记(如
p
span
)内的内容时,它可以正常工作。但是,如果选择任何标记之后的任何位置,它将抛出一个错误-
Uncaught IndexSizeError:未能在“范围”上执行“setStart”:偏移量364大于或等于节点的长度(37).
请查看此html示例-