Getting selected text with CKEditor Plugin on IE

2019-01-11 02:21发布

I have made a plugin for CKEditor, but it relies on the currently selected text.

In FF and Chrome I can use:

var selectedText = editor.getSelection().getNative();  

but this doesn't work in IE and I only get [object Object]

Any suggestions?

5条回答
狗以群分
2楼-- · 2019-01-11 02:37

Use:

editor.getSelection().getSelectedText();

Or:

CKEDITOR.instances["txtTexto"].getSelection().getSelectedText()

"txtTexto" = ID of textarea tag

查看更多
倾城 Initia
3楼-- · 2019-01-11 02:41

This is what I use:

var mySelection = editor.getSelection();

if (CKEDITOR.env.ie) {
    mySelection.unlock(true);
    selectedText = mySelection.getNative().createRange().text;
} else {
    selectedText = mySelection.getNative();
}
查看更多
Root(大扎)
4楼-- · 2019-01-11 02:42

@TheApprentice

You put it like this:

( function(){

  var getSelectedText = function(editor) {
    var selectedText = '';
    var selection = editor.getSelection();
    if (selection.getType() == CKEDITOR.SELECTION_TEXT) {
      if (CKEDITOR.env.ie) {
        selection.unlock(true);
        selectedText = selection.getNative().createRange().text;
      } else {
        selectedText = selection.getNative();
      }
    }
    return(selectedText);
  }

...

with a call like this:

onShow: function() {
  // Get the element currently selected by the user
  var editor = this.getParentEditor();
  var selectedContent = getSelectedText(editor);
查看更多
戒情不戒烟
5楼-- · 2019-01-11 02:46

To those who want to prefill fields with a selection, just do it like that and safe yourself a long journey.

onShow: function() {
    this.setValueOf( 'tab-id', 'field-id', editor.getSelection().getSelectedText().toString() );
},

Have a nice day!

查看更多
我只想做你的唯一
6楼-- · 2019-01-11 02:46

In the newer versions of CKEDITOR, there seems to be a way easier method:

var selectedHTML = editor
                      .getSelectedHtml()
                      .getHtml(); //result: <p>test</p>
查看更多
登录 后发表回答