2012-08-02 89 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标记。