我正在使用 tinymce 4.0.1,它会在您开始输入或按回车键时自动添加 p 标签。如何动态删除这些 p 标签,然后将内容重新插入到编辑器中。
您需要将以下行添加到您的 init 语句中
forced_root_block : ""
所以,你的完整代码将是这样的:
<script>tinymce.init({forced_root_block : "",selector:'textarea'});</script>
Tinymce 需要一个根块元素,默认情况下是一个段落,以便能够设置内容样式。因此,删除这些段落只会导致所有内容被包装到一个段落中,因为 tinymce 被迫将其用作根块元素。
您可以通过在 tinymce 设置中添加
forced_root_block : false
来删除“p”标签,或者您可以通过 statusbar: false
隐藏状态栏
怎么样
$("p").each(function(){$(this).parent().append($(this).html()); $(this).remove()})
仅在调用 javascript 时添加:
forced_root_block : false
您需要将以下行添加到您的 init 语句中。
forced_root_block : ""
当你显示它时,你能简单地调整 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.
希望这有帮助。
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(例如)。
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' );
elementpath 配置隐藏它。
文档:https://www.tiny.cloud/docs/configure/editor-appearance/#elementpath 参考:https://github.com/tinymce/tinymce-docs/issues/1373#issuecomment-578710877
$("p").remove();