【问题标题】:tinymce setContent removes new linestinymce setContent 删除新行
【发布时间】:2012-08-08 10:50:54
【问题描述】:

如果我在 textarea 中预加载了内容,那么我会将新行转换为“br”标签。

但是,如果我尝试使用其 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

【问题讨论】:

  • 您的问题是什么?你想达到什么目标?
  • 对不起,jsut编辑的问题,预期结果在底部

标签: tinymce


【解决方案1】:

如何替换新行以在初始内容集上换行:

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 />');
            }
        });
    }
});

【讨论】:

  • 使用 4.2 我不得不稍微调整一下,但这个答案让我走上了正轨。 tinymce.init({setup: function (editor) {editor.on('BeforeSetContent', function (contentEvent) {contentEvent.content = contentEvent.content.replace(/\r?\n/g, '
    ' );})}});
  • Patrick Graham 在上面的评论中列出的调整对我有用。谢谢帕特里克!我已经解决这个问题两天了,像“remove_linebreaks”这样的小修复都不适合我。
  • 注意其他人看这个:你必须有forced_root_block: false才能工作(在示例中设置为这样,但它有点隐藏)
【解决方案2】:

请参阅我的解决方案。我创建了一个 tinymce 小提琴:http://fiddle.tinymce.com/vTbaab

【讨论】:

  • 没错!将新行替换为
    s 对我来说是一个可以接受的解决方案
  • 很高兴能够提供帮助
【解决方案3】:

你需要做的就是添加

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

这在他们的help中有很好的记录。

【讨论】:

    【解决方案4】:

    我尝试了许多其他方法来尝试使其正常工作。它源于原始开发人员没有正确声明帖子类型。

    之前:

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

    之后:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多