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


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

    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
        {

        }
}
        });

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


10476
2017-10-21 08:22


起源



答案:


尝试

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

要么

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

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);
}

12
2017-10-21 08:30



谢谢Thariama。但是如果我选择带有相同标签的文本,它只提供文本,如果我选择带有2个不同标签的文本,它将为html提供内容 - Warrior
嗯,这可能是因为选择在第一种情况下仅包含文本,但在第二种情况下(在选择内部)中存在标签。它给你完整的HTML(完整的标签)? - Thariama
你正在获得带标签的完整html - Warrior
一个不太好的方法可能是扩大选择以获取标签并在之后剪切它 - Thariama
标签不是我创建标签的html标签。如果我选​​择了一个有效的html标签的内容,那么整个html将会到来 - Warrior


答案:


尝试

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

要么

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

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);
}

12
2017-10-21 08:30



谢谢Thariama。但是如果我选择带有相同标签的文本,它只提供文本,如果我选择带有2个不同标签的文本,它将为html提供内容 - Warrior
嗯,这可能是因为选择在第一种情况下仅包含文本,但在第二种情况下(在选择内部)中存在标签。它给你完整的HTML(完整的标签)? - Thariama
你正在获得带标签的完整html - Warrior
一个不太好的方法可能是扩大选择以获取标签并在之后剪切它 - Thariama
标签不是我创建标签的html标签。如果我选​​择了一个有效的html标签的内容,那么整个html将会到来 - Warrior


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

3
2017-07-21 23:42