How to initialize CKEditor textarea line breaks as like html textarea line breaks

两盒软妹~` 提交于 2021-01-28 04:43:06

问题


  1. HTML textareas can recognize line breaks (enter button) and i can save the element's text value into database. Then i can get those data, set it on any textarea, i still have line breaks (no problem until here).
  2. CKEditor textarea puts <p> or <br /> after line breaks. When i remove these tags, CKEditor cannot recognize there's a line break.

Purpose : I'm using a word counter plugin with CKEditor, so i need to use CKEditor as like a textarea. I need the plain text value.

I tried these so far :

CKEDITOR.instances.editor1.document.getBody().getText()

Yes i can get the text value correctly, i put this value on DB, but when i reload my page, the line breaks are gone. Because ckeditor works only with html tags ?

config.autoParagraph = false;


This one just removes the <p> tag at the beginning of the content.

I need plain text+line breaks that's it. Any suggestions ? Or should i write down my own classes ?


回答1:


Array.prototype.map
            .call(CKEDITOR.instances.editor1.document.getBody().$.childNodes,
                 function(item){
                  return item.innerText;
                  })
            .join("\n");

Just a crazy answer to convert <p> blocks to a string separated by \n



来源:https://stackoverflow.com/questions/33104421/how-to-initialize-ckeditor-textarea-line-breaks-as-like-html-textarea-line-break

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