2012-08-08 69 views
3

如果我在textarea中预先加载了内容,那么我会将新行转换为“br”标记。tinymce setContent删除新行

但是,如果我尝试使用它的setContent函数动态(不粘贴)tinymce textarea设置内容缺少新行。

我使用v.3.4.7,试过v.3.5.6(最新的),它甚至在页面加载时也删除了新的行。

<script type="text/javascript"> 
    tinyMCE.init({ 
     mode: "textareas", 
     editor_selector: "EmailBody", 
     theme: "advanced", 
     language: "en", 
     charLimit: 10, 
     plugins: "table,advhr,advlink,insertdatetime,preview,print,contextmenu,paste,directionality", 
     theme_advanced_buttons1_add: "fontselect,fontsizeselect", 
     theme_advanced_buttons2_add: "separator,insertdate,inserttime,preview,zoom,separator,forecolor,backcolor", 
     theme_advanced_buttons2_add_before: "pastetext,pasteword,separator", 
     theme_advanced_buttons3_add_before: "tablecontrols,separator", 
     theme_advanced_buttons3_add: "advhr,separator,ltr,rtl,separator", 
     theme_advanced_toolbar_location: "top", 
     theme_advanced_toolbar_align: "left", 
     theme_advanced_statusbar_location: "bottom", 
     plugi2n_insertdate_dateFormat: "%Y-%m-%d", 
     plugi2n_insertdate_timeFormat: "%H:%M:%S", 
     paste_use_dialog: false, 
     theme_advanced_resizing: false, 
     theme_advanced_resize_horizontal: true, 
     paste_auto_cleanup_on_paste: true, 
     paste_convert_headers_to_strong: false, 
     paste_remove_spans: true, 
     width: "100%", 
     paste_remove_styles: true, 
     valid_elements: "a[href|target=_blank],strong/b,div[align],p,br,i,u", 
     content_css: "/css/tinymce_bigger_default_font.css", 
     forced_root_block: false, 
     force_br_newlines: true, 
     force_p_newlines: false, 
     apply_source_formatting: false, 
     remove_linebreaks: false, 
     convert_newlines_to_brs: true 
    }); 
</script> 

function Click() 
{ 
    var text = document.getElementById("preText").innerText; 
    tinyMCE.activeEditor.setContent(text); 
} 

<pre id="preText">Text 

Text 
</pre> 

的结果必须是如下:

Text 

Text 

而是我得到:

TextText 
+0

你有什么问题吗?你想达到什么目的? – Thariama 2012-08-08 10:54:16

+0

对不起,jsut编辑问题,预计结果正好在底部 – 2012-08-08 10:56:28

回答

8

如何更换新的防线初始内容套系:

convert_newlines_to_brsremove_linebreaks参数已经从TinyMCE的(检查你的TinyMCE的源代码)中删除。

要恢复convert_newlines_to_brs功能使用该代码(这是从以前版本的TinyMCE的拍摄):

tinyMCE.init({ 
    setup : function(ed) { 
     ed.onBeforeSetContent.add(function(ed, o) { 
      if (o.initial) { 
       o.content = o.content.replace(/\r?\n/g, '<br />'); 
      } 
     }); 
    } 
}); 
+0

http://www.tinymce.com/wiki.php/api4:event.tinymce.Editor.BeforeSetContent – Meglio 2014-10-20 18:32:33

+1

使用4.2我必须调整这一点,但这个答案让我走在正确的轨道上。 ({setup:function(editor){editor.on('BeforeSetContent',function(contentEvent){contentEvent.content = contentEvent.content.replace(/ \ r?\ n/g''
'); })}}); – 2015-12-30 21:04:17

+0

帕特里克格雷厄姆在他上面评论中列出的调整对我有效。感谢Patrick!我一直在这个问题上工作了两天,没有像'remove_linebreaks'这样的小修补程序能为我工作。 – 2016-06-10 16:01:01

3

看我为这个解决方案。我创建了一个TinyMCE的小提琴:http://fiddle.tinymce.com/vTbaab

+1

!替换新行
s对我来说是可接受的解决方案 – 2012-08-08 14:11:48

+0

很高兴能够帮助 – Thariama 2012-08-08 14:28:51

0

所有你需要做的是添加

tinyMCE.init({ 
     ... 
     remove_linebreaks : false 
}); 

这是很好的记录在其help

0

我尝试了很多其他的事情来试图让这个工作。它源于最初的开发者未正确声明帖子类型。

BEFORE:

'supports' => array('title','slug'), 

AFTER:

'supports' => array('title','slug','editor'),