Getting selected html content in tinymce editor

2019-02-12 08:03发布

问题:

I have created a custom button using this code

    setup : function(ed) {
    ed.addButton('Tittle', {
                title : 'Tittle',
                image : './images/T.jpg',
                onclick : function() {
                ed.focus();
            var c = ed.selection.getNode().nodeName;
        if(c!="TITTLE")
        {
             ed.selection.setContent('<tittle>' + ed.selection.getContent() + '</tittle>');

        }
        else
        {

        }
}
        });

When a user select a text and click on the new button, i want to add a <title> tag at beginning and ending, if <tittle> tag is not their.If <tittle> tag is already their in the selected text i want to remove the tag

回答1:

try

selection.getContent({format : 'text'});

or

selection.getContent({format : 'html'});

http://www.tinymce.com/wiki.php/API3:method.tinymce.dom.Selection.getContent

EDIT: To achieve what you want you could do:

if(c!="TITTLE") {

  node = ed.selection.getNode();

  with(document.getElementById(iframe_id).contentWindow){
      var newElement = document.createElement("tittle");
      newElement.innerHTML = node.innerHTML;
  }

  node.parentNode.replaceChild(newElement, node);

}
else {

  node = ed.selection.getNode();

  with(document.getElementById(iframe_id).contentWindow){
      var newElement = document.createTextNode(node.innerHTML);
  }

  node.parentNode.replaceChild(newElement, node);
}


回答2:

var node = tinyMCE.activeEditor.selection.getContent();
tinyMCE.execCommand('mceReplaceContent', false, %your value, can use node%");


标签: get tinymce