How to prevent the <p> tag from wrapping around my input with tinymce in Rails?

和自甴很熟 提交于 2019-12-21 23:30:04

问题


By default, the tinymce input gets passed to the DOM as a paragraph tag:

I would like to remove that element wrapper so that tinymce passes exactly what I entered in the text editor. How do I do that ? Please if you provide a code, can you also let me know where that code gets added ? Regards !!!


回答1:


Actually I solved my problem. All I had to do was change the styling for paragraph tag :

p {margin: 0; padding: 0;}



回答2:


You need to specify the forced_root_block to false. However the documentation states that not having your root block as a <p> tag can cripple the editors behaviour. Newlines will be spaced with <br> tags instead.

tinyMCE.init({
    selector: 'textarea',
    forced_root_block: false
});

See the documentation here




回答3:


I strip out those pesky things with gsub and regex like this:

<%= @event.desc_long.gsub(/^\<p\>/,"").gsub(/\<\/p\>$/,"") %>

First .gsub removes the <p> at the start of the TinyMCE string, and the second one removes the </p> at the end. Working great for me. This would work for any language that uses regex (gsub is for rails). JavaScript example:

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

Hope this helps!

EDIT:

Re: where to put it. You leave what TinyMCE puts in your database alone. Add the above only when you display it (in the view, e-mail whatever).




回答4:


In case you just want to get rid of margins:

tinymce.init({
...
setup: function(ed) { 
  ed.on('init', function() { 
    var doc = this.getDoc().getElementById("tinymce");
    doc.style.margin = 0;
  });
},
});


来源:https://stackoverflow.com/questions/32596205/how-to-prevent-the-p-tag-from-wrapping-around-my-input-with-tinymce-in-rails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!