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

前端 未结 11 1891
挽巷
挽巷 2020-12-06 04:46

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

11条回答
  •  一向
    一向 (楼主)
    2020-12-06 05:08

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

    var str = "{TinyMCE HTML string}"; /* however you get it */
    str = str.replace(/^\/,"").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.

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

    ^

    - find

    at the start of the string (^) and replace it with nothing.

    $ - find

    at the end of the string ($) and replace it with nothing.

    Hope this helps.

提交回复
热议问题