简体   繁体   English

删除html标签,但保留换行符

[英]Removing html tags but keep breaklines

We need to make up a text with html markup and without (for mails) So i have ckeditor to makeup the text with html markup, but when i delete the markup with a script al breaklines are gone, because the script strips the <p> tags. 我们需要用html标记组成一个文本,并且不带(用于邮件),所以我已经让ckeditor用html标记组成文本,但是当我删除带有脚本的标记时,所有的换行符都消失了,因为脚本会删除<p>标签。 and i'm left with one long string of text instead of a descent layout. 我只剩下一长串文本,而不是下降的布局。 Anyone an idea how i can keep the breaklines? 任何人都知道我如何保持突破线吗?

function strip(){
        var html = CKEDITOR.instances.Maintext.getData();
        var text = $(html).text(); 
        document.getElementById('nohtmltext').value = text;
    }

I found a solution with some help from the answers given. 我从给出的答案中找到了一些帮助的解决方案。

function strip(){
            var html = CKEDITOR.instances.Maintext.getData();
            html = html.replace(/<p>/g,'');
            html = html.replace(/<\/p>/g,'\n\n');
            var tempDiv = document.createElement('div');
            tempDiv.innerHTML = html;
            var text = $(tempDiv).text(); 
            document.getElementById('nohtmltext').value = text;
        }

I know this is not the most beautiful bit of code i'v ever written, but it does exactly what i want. 我知道这不是我编写过的最漂亮的代码,但是它确实可以实现我想要的功能。 Thanks for the help 谢谢您的帮助

一种解决方案是用“ \\ n”替换所有“ p-tag”。

html.replace('<p>','').replace('</p>','\n\n').text(); // not tested

Wrong: 错误:

function strip(){
        var html = CKEDITOR.instances.Maintext.getData();
        var text = $(html).text(); 
        document.getElementById('nohtmltext').value = text;
}

Good: 好:

function strip(){
    var html = CKEDITOR.instances.Maintext.getData();
    document.getElementById('nohtmltext').innerHTML = html;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM