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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17376595/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 10:35:45  来源:igfitidea点击:

How can I remove p tags that are auto added within tinymce

javascripthtmldomtinymce

提问by Juan Gonzales

I am using tinymce 4.0.1 and it automatically adds p tags when you either start typing or hit enter. How can I dynamically remove these p tags and then reinsert the content into the editor.

我使用的是 tinymce 4.0.1,当您开始输入或按 Enter 时,它会自动添加 p 标签。如何动态删除这些 p 标签,然后将内容重新插入编辑器。

采纳答案by Thariama

Tinymce needs a root block element, which is a paragraph by default, in order to be able to style content. So removing those paragraphs will only result in all content being wrapped into one single paragraph because tinymce is forced to use this as root block element.

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

回答by turco_7

you need to add the following line to your init statement

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

forced_root_block : ""

So, your complete code will be like this:

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

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

回答by Fadel

You can remove "p" tag by adding forced_root_block : falseto your tinymce setup or you can hide the status bar by statusbar: false

您可以通过添加forced_root_block : false到 tinymce 设置来删除“p”标签,也可以通过以下方式隐藏状态栏statusbar: false

回答by gnuger

How about

怎么样

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

回答by user3212640

You need to add the following line to your init statement.

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

forced_root_block : ""

回答by Ed Birm

Can you simply tweak what TinyMCE puts into the database when you display it?See my post for the same thing for Rails.

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

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

Here you are removing the beginning and ending p tag of the whole TinyMCE HTML when you display it. Doesn't mess with other p tags or the TinyMCE config.

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

Explanation of the regex expression (removed \'s for ease of reading):

正则表达式的解释(为了便于阅读,删除了 \'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.

Hope this helps.

希望这可以帮助。

回答by Mikle Gardener

Insert in theme functions.phpthe following code:

在主题中插入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;
}

If you want replace root "p" tag with some else, replace false with "div" (for example).

如果您想用其他标签替换根 "p" 标签,请将 false 替换为 "div"(例如)。

回答by Nateno

Add this to your functions.php file and the standard

将此添加到您的functions.php文件和标准

tags will be removed by adding some parameters to the tiny_mce_before_init hook. If you want to see how it works, you can read further on this page: https://codex.wordpress.org/TinyMCE

将通过向 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' );

回答by Rodrigo Prazim

Add this only in call javascript:

仅在调用 javascript 中添加此内容:

forced_root_block : false

回答by Phan ???c Bi?nh

Are you looking for: forced_root_block : '', force_br_newlines : true, force_p_newlines : false,

你在寻找:forced_root_block : '', force_br_newlines : true, force_p_newlines : false,