2012-08-02 85 views
8

我有一些textareas,它們都與tinyMCE。一個textarea與tinyMCE的setContent

我想設置特定textarea的內容,但我找不到如何。

我已經tryed這一點:

tinyMCE.get('title').setContent(selected_article_title); 

這裏是我的textarea:

<textarea style="width: 95%;" name="Title" id="title"></textarea> 

在這裏,我的TinyMCE的初始化:

tinyMCE.init({ 
// General options 
mode : "specific_textareas", 
theme : "advanced", 
width: "100%", 
plugins : "pagebreak,paste,fullscreen,visualchars", 

// Theme options 
theme_advanced_buttons1 : "code,|,bold,italic,underline,|,sub,sup,|,charmap,|,fullscreen,|,bullist,numlist,|,pasteword", 
theme_advanced_buttons2 :"", 
theme_advanced_buttons3 :"", 
theme_advanced_buttons4 :"", 
theme_advanced_toolbar_location : "top", 
theme_advanced_toolbar_align : "left", 
theme_advanced_statusbar_location : "bottom", 
valid_elements : "i,sub,sup", 
invalid_elements : "p, script", 
editor_deselector : "mceOthers" 
}); 

我不知道這是爲什麼不工作,我使用tinyMCE網站的例子http://www.tinymce.com/wiki.php/API3:method.tinymce.Editor.setContent

回答

10

我有(誰給我一些元素thans到Thariama)解決方案

要使用TinyMCE的設置textarea的內容,我們HEVE初始化TinyMCE的前填寫的textarea。此外,答覆如下:

  1. 創建textarea的:

    <textarea style="width: 95%;" name="Title" id="title"></textarea> 
    
  2. 設置textarea的內容:

    $('#title').html(selected_article_title); 
    
  3. 初始化TinyMCE的:

    tinyMCE.init({ 
    // General options 
    mode : "specific_textareas", 
    theme : "advanced", 
    width: "100%", 
    plugins : "pagebreak,paste,fullscreen,visualchars", 
    
    // Theme options 
    theme_advanced_buttons1 : "code,|,bold,italic,underline,|,sub,sup,|,charmap,|,fullscreen,|,bullist,numlist,|,pasteword", 
    theme_advanced_buttons2 :"", 
    theme_advanced_buttons3 :"", 
    theme_advanced_buttons4 :"", 
    theme_advanced_toolbar_location : "top", 
    theme_advanced_toolbar_align : "left", 
    theme_advanced_statusbar_location : "bottom", 
    valid_elements : "i,sub,sup", 
    invalid_elements : "p, script", 
    editor_deselector : "mceOthers" 
    }); 
    

完成了!請享用。

1

使用這種

tinyMCE.get('title').setContent(selected_article_title); 

將無法​​正常工作。它會設置你的編輯器內容。

要設置編輯器源HTML元素(textarea的),你將需要設置它直接使用

$('#title').html(selected_article_title); 

你需要知道你的編輯器是不是一樣的textarea的!

+0

謝謝@Thariama,但這不適合我。我的textarea仍然是空的。 – 2012-08-03 06:17:23

6

對於TinyMCE的版本4,

tinymce.get('title').setContent(selected_article_title); 

的作品就好了 - 也初始化編輯器後。

+0

這對我有用 – 2016-08-25 13:23:12

0

如果您設置的內容包含mso標記(從Outlook2013生成的html內容,其中包含編號列表,例如),您將放棄列表元素。通過設置tinymce.activeEditor.setContent(foo)首先設置textarea內容,然後初始化tinymce給出相同的結果,我們看不到正確的列表元素,它們是左對齊的。但是,如果我們使用setContent(foo, { format:'raw' })設置,則可以正確地查看列表元素。爲什麼?

6

我認爲這將解決您的問題

它工作得很好TinyMCE的五:4。

// Sets the HTML contents of the activeEditor editor 
tinyMCE.activeEditor.setContent('<span>some</span> html'); 

// Sets the raw contents of the activeEditor editor 
tinyMCE.activeEditor.setContent('<span>some</span> html', {format : 'raw'}); 

// Sets the content of a specific editor (my_editor in this example) 
tinyMCE.get('my_editor').setContent(data); 

// Sets the bbcode contents of the activeEditor editor if the bbcode plugin was added 
tinyMCE.activeEditor.setContent('[b]some[/b] html', {format : 'bbcode'}); 

的鏈接代碼是TinyMCE setContent

0

與TinyMCE的版本4以下的作品,並沒有顯示出編輯的文本區域,而它的被拖拽:

function initializeEditors() { 
    var editorContent = {}; 
    $("#photo-list").sortable({ 
     start: function (e, ui) { 
      $('.attachment-info').each(function() { 
       var id = $(this).attr('id'); 
       editorContent[id] = tinyMCE.get(id).getContent(); 
      }); 
     }, 
     stop: function (e, ui) { 
      $('.attachment-info').each(function() { 
       var id = $(this).attr('id'); 
       tinymce.execCommand('mceRemoveEditor', false, id); 
       tinymce.execCommand('mceAddEditor', true, id); 
       tinyMCE.get(id).setContent(editorContent[id]); 
      }); 
     } 
    }); 
} 
0
$('#title').html(selected_article_title); 

確保selected_article_title不包含任何html標記。