2012-12-24 15 views
14

Sono un principiante su Javascript/TinyMCE e cerco di capire come è possibile ottenere il contenuto HTML dall'editor e mostrarlo con una semplice funzione alert().Come estrarre il contenuto HTML da TinyMCE Editor

ho questa configurazione minimalista sulla mia pagina HTML:

<div id="tiny"> 
<script type="text/javascript"> 
tinyMCE.init({ 
     // General options 
     mode : "specific_textareas", 
     editor_selector : "mceEditor" 
}); 
</script> 
</div> 

<form method="post" action="somepage"> 
     <textarea id="myarea1" class="mceEditor">This will be an editor.</textarea> 
</form> 

Sul TinyMCE Website, hanno spiegato che devo usare questo:

// Get the HTML contents of the currently active editor 
console.debug(tinyMCE.activeEditor.getContent()); 

E here troppo

tinymce.activeEditor.getContent() 

Non so perché non funziona

Qualcuno ha un'idea?

risposta

21

Non so il motivo per cui non funziona

Non funziona perché

console.debug(tinyMCE.activeEditor.getContent()); 

tinymce.activeEditor.getContent(); 

queste affermazioni non vengono eseguiti.

cercare di seguire questa FIDDLE ....

tinyMCE.init({ 
     // General options 
     mode : "specific_textareas", 
     editor_selector : "mceEditor" 
}); 

funzione per ottenere contenuti ....

function get_editor_content() { 
    // Get the HTML contents of the currently active editor 
    console.debug(tinyMCE.activeEditor.getContent()); 
    //method1 getting the content of the active editor 
    alert(tinyMCE.activeEditor.getContent()); 
    //method2 getting the content by id of a particular textarea 
    alert(tinyMCE.get('myarea1').getContent()); 
} 

ottenere il contenuto del montatore scatto del tasto ...

<button onclick="get_editor_content()">Get content</button> 
+0

Grazie mille per il tuo aiuto. È molto ben spiegato, capisco ora :) – Ikes

2

Forse è il caso? La tua variabile è tinyMCE, ma stai chiamando getContent() su tinymce. JS è case sensitive;)

1

Ero alla ricerca di una soluzione e ho provato alcuni dei precedenti, poi sono andato a cercare di più sulla documentazione di tinymce e trovato questo per essere efficace.
Utilizzando il piccolo MCE 4

function getHTML() 
{ 
    tinymce.activeEditor.on('GetContent', function(e) { 
    console.log(e.content); 
    }); 
} 

basta chiamare che funzione con un onclick e vedere quali sono i risultati ...
La mia fonte è: http://www.tinymce.com/wiki.php/api4:class.tinymce.ContentEvent

0

TinyMCE crea un iframe con il formato '#textareaid' + '_ ifr' Quindi usando jquery possiamo interrogare i contenuti HTML dell'area di testo che ti piace

l'id iframe sarà il tuo ID textarea con "_ifr" aggiunto a quello. Così puoi estrarre contenuti HTML da tinyMce

$('#textareaId_ifr').contents().find("html").html(); 
Problemi correlati