如何删除在 tinymce 中自动添加的 p 标签

问题描述 投票:0回答:12

我正在使用 tinymce 4.0.1,它会在您开始输入或按回车键时自动添加 p 标签。如何动态删除这些 p 标签,然后将内容重新插入到编辑器中。

javascript html dom tinymce
12个回答
57
投票

您需要将以下行添加到您的 init 语句中

forced_root_block : ""

所以,你的完整代码将是这样的:

    <script>tinymce.init({forced_root_block : "",selector:'textarea'});</script>

16
投票

Tinymce 需要一个根块元素,默认情况下是一个段落,以便能够设置内容样式。因此,删除这些段落只会导致所有内容被包装到一个段落中,因为 tinymce 被迫将其用作根块元素。


8
投票

您可以通过在 tinymce 设置中添加

forced_root_block : false
来删除“p”标签,或者您可以通过
statusbar: false

隐藏状态栏

2
投票

怎么样

$("p").each(function(){$(this).parent().append($(this).html()); $(this).remove()})

2
投票

仅在调用 javascript 时添加:

forced_root_block : false

1
投票

您需要将以下行添加到您的 init 语句中。

forced_root_block : ""

1
投票

当你显示它时,你能简单地调整 TinyMCE 放入数据库的内容吗? 请参阅我的帖子以了解 Rails 的相同内容

var str = "{TinyMCE HTML string}"; /* however you get it */ str = str.replace(/^\<p\>/,"").replace(/\<\/p\>$/,"");

在这里,当您显示它

时,您将删除整个 TinyMCE HTML 的开始和结束 p 标记。不会与其他 p 标签或 TinyMCE 配置混淆。

正则表达式的解释(为便于阅读删除了\'s):

^<p> - find <p> at the start of the string (^) and replace it with nothing. </p>$ - find </p> at the end of the string ($) and replace it with nothing.

希望这有帮助。


0
投票
在主题

functions.php

中插入以下代码:

add_filter('tiny_mce_before_init', 'my_switch_tinymce_p_br'); function my_switch_tinymce_p_br($settings) { $settings['forced_root_block'] = false; return $settings; }

如果你想用其他的替换根“p”标签,用“div”替换 false(例如)。


0
投票
将其添加到您的 functions.php 文件中,通过向 tiny_mce_before_init 挂钩添加一些参数,将删除标准的

标签。 如果你想看看它是如何工作的,你可以在这个页面上进一步阅读:

https://codex.wordpress.org/TinyMCE

//////////////////////////////////////////////////////////////////////// //////////REMOVE STANDARD <P> FROM TINYMCE EDITOR///////////////////////// /////////////////////////////////////////////////////////////////////// function my_format_TinyMCE( $in ) { $in['forced_root_block'] = ""; $in['force_br_newlines'] = TRUE; $in['force_p_newlines'] = FALSE; return $in; } add_filter( 'tiny_mce_before_init', 'my_format_TinyMCE' );
    

0
投票
您在寻找: forced_root_block : '', force_br_newlines :是的, force_p_newlines :假,


0
投票
您可以使用

elementpath 配置隐藏它。

文档:

https://www.tiny.cloud/docs/configure/editor-appearance/#elementpath 参考:https://github.com/tinymce/tinymce-docs/issues/1373#issuecomment-578710877


-6
投票
如果你使用 jQuery 可以做:

$("p").remove();
    
© www.soinside.com 2019 - 2024. All rights reserved.