如何在ckeditor中包装选定的文本

nin*_*ner 9 javascript ckeditor

我想CKEditor在一个<p>元素中包含选定的单词.

从:

<p>This is a paragraph. And this is Selected text.</p>
Run Code Online (Sandbox Code Playgroud)

至:

<p>This is a paragraph. And this is</p>
<p class="myclass">Selected text.</p>
Run Code Online (Sandbox Code Playgroud)

我找到了一些代码:

( function() {
    CKEDITOR.plugins.add( 'qna', { 
        init: function( editor ) {
            editor.addCommand( 'insertQnA', { 
                exec : function( editor ) {    
                    if(CKEDITOR.env.ie) {
                        editor.getSelection().unlock(true); 
                            var selected_text = editor.getSelection().getNative().createRange().text; 
                    } else { 
                        var selected_text = editor.getSelection().getNative();
                    }
                    editor.insertHtml('[before]' + selected_text + '[after]'); 
                } 
            }); 
            editor.ui.addButton( 'qna', { 
                label: 'Insert QnA', 
                command: 'insertQnA', 
                icon: this.path + 'images/qna.png'
            }); 
        } 
    });
})();
Run Code Online (Sandbox Code Playgroud)

我想更换[before]和[after]用<p class"myclass">和</p>,但它不工作.

我是JS/Jquery的新手.我希望你能为我解释一下.

编辑:来自Spon的回复.

( function() {
  CKEDITOR.plugins.add( 'qna', { 
    init: function( editor ) {
      editor.addCommand( 'insertQnA', { 
        exec : function( editor ) {    
          editor.applyStyle(new CKEDITOR.style({
            Element : 'p', 
            Attributes : { class : 'Myclass' }, 
            Styles : { color : '#ff0000','font-family' : 'Courier'} 
          }));
        } 
      }); 
      editor.ui.addButton( 'qna', { 
        label: 'Insert QnA', 
        command: 'insertQnA', 
        icon: this.path + 'images/question.png'
      }); 
    } 
  });
})();
Run Code Online (Sandbox Code Playgroud)

由于<span>某些未知原因,上面的代码将选定的文本/单词包装在元素中.

例:

从...

<p>This is a paragraph. And this is Selected text.</p>
Run Code Online (Sandbox Code Playgroud)

至...

<p>This is a paragraph. And this is <span>Selected text.</span></p>
Run Code Online (Sandbox Code Playgroud)

这不是我想要的.

Spo*_*ons 21

exec : function( editor ) {
  var selected_text = editor.getSelection().getSelectedText(); // Get Text
  var newElement = new CKEDITOR.dom.element("p");              // Make Paragraff
  newElement.setAttributes({style: 'myclass'})                 // Set Attributes
  newElement.setText(selected_text);                           // Set text to element
  editor.insertElement(newElement);                            // Add Element
}
Run Code Online (Sandbox Code Playgroud)

这将解决它..这是您可以看到的Exec部分.