在tinymce编辑器中获取所选的html内容

War*_*ior 12 get tinymce

我使用此代码创建了一个自定义按钮

    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
        {

        }
}
        });
Run Code Online (Sandbox Code Playgroud)

当用户选择文本并单击新按钮时,我想<title>在开头和结尾添加标签,如果<tittle>标签不是<tittle>他们的.如果标签已经是他们在所选文本中我想要删除标签

Tha*_*ama 13

尝试

selection.getContent({format : 'text'});
Run Code Online (Sandbox Code Playgroud)

要么

selection.getContent({format : 'html'});
Run Code Online (Sandbox Code Playgroud)

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

编辑: 为了实现你想要的你可以做到:

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);
}
Run Code Online (Sandbox Code Playgroud)


wba*_*ars 6

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