2010-10-21 24 views
11

He creado un botón personalizado usando este códigoConseguir contenido HTML seleccionado en el editor 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 
     { 

     } 
} 
     }); 

Cuando un usuario selecciona un texto y haga clic en el botón de nuevo, quiero añadir una etiqueta <title> en el comienzo y el final Si <tittle> etiqueta es la etiqueta no their.If <tittle> ya está en su texto seleccionado quiero quitar la etiqueta

Respuesta

12

tratar

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

o

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

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

EDIT: Para lograr lo que quiere usted podría hacer:

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

Gracias Thariama.But que está dando sólo el texto si selecciono una texto con la misma etiqueta, si selecciono un texto con 2 etiquetas diferentes, dará el html con contenido – Warrior

+1

hmm, eso podría ser porque la selección solo contiene texto en El primer caso, pero en el segundo hay etiquetas en el medio (dentro de la selección). ¿Te da el html completo (etiquetas completas)? – Thariama

+0

ya estoy obteniendo el html completo con las etiquetas – Warrior

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