Getting selected html content in tinymce editor

*爱你&永不变心* 提交于 2019-11-29 17:37:40

问题


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%");


来源:https://stackoverflow.com/questions/3985524/getting-selected-html-content-in-tinymce-editor

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!