Text 用于从CKEditor中的光标位置获取文本的函数

Text 用于从CKEditor中的光标位置获取文本的函数,text,cursor,ckeditor,Text,Cursor,Ckeditor,我试图从光标位置开始提取所有文本。以下是我正在使用的代码: originalText = editor.getData(); var startTag = "<span id=\x22Start\x22>&nbsp;</span>"; var stopTag = "<span id=\x22Stop\x22>&nbsp;</span>"; var startElement = CKEDITOR.dom.element.createF

我试图从光标位置开始提取所有文本。以下是我正在使用的代码:

originalText = editor.getData();
var startTag = "<span id=\x22Start\x22>&nbsp;</span>";
var stopTag = "<span id=\x22Stop\x22>&nbsp;</span>";
var startElement = CKEDITOR.dom.element.createFromHtml( startTag, editor.document );
editor.insertElement(startElement);
sText = editor.getData();
sText1 = sText + stopTag;
editor.setData(sText1);
// up to here, I've incapsulated the required text with span tags
// Using the replace function, I remove end tag of the Start span as well as removing    the start tag of the Stop span!
sText1 = editor.getData();
sText2 = sText1.replace("<span id=\"Start\">&nbsp;</span>", "<span id=\"Start\">");
sText2 = sText2.replace("<span id=\"Stop\">&nbsp;</span>", "</span>");
// I set the data (HTML) back to the editor
editor.setData(sText2);
//alert(sText2);
// I use the innerHTML to get the text
el = editor.document.$.getElementById("Start");
return el.innerHTML;
originalText=editor.getData();
var startTag=“”;
var stopTag=“”;
var startElement=CKEDITOR.dom.element.createFromHtml(startTag,editor.document);
编者:insertElement(startElement);
sText=editor.getData();
sText1=sText+停止标记;
编辑器.setData(sText1);
//到目前为止,我已经用span标记封装了所需的文本
//使用“替换”功能,我删除了开始跨距的结束标记,也删除了停止跨距的开始标记!
sText1=editor.getData();
sText2=sText1.replace(“,”);
sText2=sText2.replace(“,”);
//我将数据(HTML)设置回编辑器
编辑器.setData(sText2);
//警报(sText2);
//我使用innerHTML获取文本
el=editor.document.$.getElementById(“开始”);
返回el.innerHTML;
问题是:
el.innerHTML工作正常,但只有在alert()未注释的情况下!我知道setData是异步的,通过对setData()使用回调可以解决问题,但不幸的是,它对我不起作用:(

尝试的快速修复方法确实是使用回调。使用回调时,您无法返回el.innerHTML,但您必须调用外部函数。您没有显示调用此代码的代码,因此很难确定如何将此代码重构为所需的方式。下面是一个未经测试和验证的虚拟版本这只是一个关于如何使用回调的示例

function extractTextStartingFromCursorPosition(yourCallback) {
    originalText = editor.getData();
    var startTag = "<span id=\x22Start\x22>&nbsp;</span>";
    var stopTag = "<span id=\x22Stop\x22>&nbsp;</span>";
    var startElement = CKEDITOR.dom.element.createFromHtml( startTag, editor.document );
    editor.insertElement(startElement);
    sText = editor.getData();
    sText1 = sText + stopTag;

    editor.setData(sText1, function() {
        // up to here, I've incapsulated the required text with span tags
        // Using the replace function, I remove end tag of the Start span as well as removing    the start tag of the Stop span!
        sText1 = editor.getData();
        sText2 = sText1.replace("<span id=\"Start\">&nbsp;</span>", "<span id=\"Start\">");
        sText2 = sText2.replace("<span id=\"Stop\">&nbsp;</span>", "</span>");

        // I set the data (HTML) back to the editor
        editor.setData(sText2, function() {
            // I use the innerHTML to get the text
            el = editor.document.$.getElementById("Start");
            yourCallback(el.innerHTML);
        });
    });
}
现在您可以这样使用它:

var html = extractTextStartingFromCursorPosition();
doSomethingWithHtml(html);
extractTextStartingFromCursorPosition(function(html) {
    doSomethingWithHtml(html);
});

虽然我仍然觉得用其他方法可以更好地实现这一功能,但我没有时间为同样的需求测试/重构一个新的解决方案。

你想做什么?意思是你的重复工作是什么?我认为这里的答案是完全重构你的做事方式。做得好Nenotlep,就是这样,我只需要在第二次回调中使用另一个setData回调将编辑器初始化为其原始内容。顺便说一句,如果这听起来很琐碎,很抱歉,但我如何在函数之外提取结果?再次感谢。@Mushahidi听到这个消息很高兴!实际上这是一个非常好的问题,一点也不琐碎。这真的取决于你你的情况和你想如何使用它。我想你说的结果是指
el.innerHTML
,对吗?你想用它做什么?我会在
doSomethingWithHtml()中插入你需要使用它的所有内容
函数,但这实际上取决于具体情况。是的,我的意思是el.innerHTML。你说的不琐碎很好,因为我很难重构它。我希望我可以将所有内容都放在doSomethingWithHtml()中,但这不是一个选项!不管怎样,在你的帮助下,我现在向前迈出了一步。