最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to get text data from froala editor? - Stack Overflow

programmeradmin3浏览0评论

Currently I use $('div#edit').froalaEditor('html.get') to retrieve the html data inside editor, but it sucks when it es to process / store the text data in my backend because of all the p tags and &npsb; symbols in raw html string.

To be honest they do not even pass to the database without losing parts of the data. Is there a way or an api to directly extract the text data as a string with proper symbols like "\n\t" from the froala editor?

Currently I use $('div#edit').froalaEditor('html.get') to retrieve the html data inside editor, but it sucks when it es to process / store the text data in my backend because of all the p tags and &npsb; symbols in raw html string.

To be honest they do not even pass to the database without losing parts of the data. Is there a way or an api to directly extract the text data as a string with proper symbols like "\n\t" from the froala editor?

Share Improve this question edited Oct 4, 2018 at 20:01 Stefan Falk 25.6k61 gold badges218 silver badges412 bronze badges asked Jun 30, 2017 at 20:03 zzlynzzlyn 951 gold badge3 silver badges10 bronze badges 1
  • Have you tried $('div#edit').innerText? See also stackoverflow./a/5002618/826983 – Stefan Falk Commented Oct 4, 2018 at 20:01
Add a ment  | 

5 Answers 5

Reset to default 2
  1. If you dont like p tags you should use the enter option:

    $('div#froala-editor-br').froalaEditor({
      enter: $.FroalaEditor.ENTER_BR
    });
    
  2. If you really would like to remove all tags you could use jQuery text method

    jQuery($('div#edit').froalaEditor('html.get')).text()
    
  3. Or you could use HTML options

    var html = $('div#edit').froalaEditor('html.get') ;
    var div = document.createElement("div");
    div.innerHTML = html;
    alert(div.innerText); 
    

Here is the simplestst way to do it.

  <textarea  name="text"></textarea>
  <script>
    new FroalaEditor('textarea',{
      heightMin: 400,  // customize minimum hight
      heightMax: 250   // customize maximum hight
    });
  </script>

  or

  <textarea id="editor"  name="text"></textarea>
  <script>
    new FroalaEditor('#editor',{
      heightMin: 400,  // customize minimum hight
      heightMax: 250   // customize maximum hight
    });
  </script>

if you are using post method or get method put this code into form and you get this data as a request.POST['text'].

There is http://textversionjs./ library which can be used to convert HTML to plain text. It also keeps new lines.

<script src="textversion.js"></script>
<script>
  var html = $('div#edit').froalaEditor('html.get');
  var text = createTextVersion(html);
</script>

Honestly, the easiest way to do this is

var body = $('#body').froalaEditor('html.get');
alert($(body).text());

You can use stringify() method in JSON to convert the HTML form to a text form.

<script>
    var html = $('div#edit').froalaEditor('html.get');
    var text = JSON.stringify(html)     
</script>

And you can store them in any backend and use them later.

发布评论

评论列表(0)

  1. 暂无评论