【问题标题】:removing HTML from Text [duplicate]从文本中删除 HTML [重复]
【发布时间】:2014-05-07 18:41:00
【问题描述】:

我正在从服务器返回一些文本,其中包含我需要的文本周围的 HTML。使用 javascript/jquery,删除所有 html 标记的最佳方法是什么,所以我记录或保存的只是文本:“这是会议的正文和文本。”

谢谢 - 下面是 html 的样子。我一直卡在退回

> <html><head><meta http-equiv="Content-Type" content="text/html;
> charset=utf-8"><meta name="Generator" content="Microsoft Exchange
> Server"><!-- converted from rtf --><style><!-- .EmailQuote {
> margin-left: 1pt; padding-left: 4pt; border-left: #800000 2px solid; }
> --></style></head><body><font face="Calibri" size="2"><span style="font-size:11pt;">             <div>This is the body and text of the
> meeting.</div><div>&nbsp;</div></span></font></body></html>

【问题讨论】:

  • 试试var str=$('body').text();
  • 如果我在上面的 HTML 中使用该示例,我会返回:标题!这是会议的正文和正文。

标签: javascript jquery html


【解决方案1】:

使用正则表达式,您可以从您的文本中删除 html 标签。

javascript:

    $('textarea').on('input', function (){
    $('div').text(sanitize(this.value));
});

var protos = document.body.constructor === window.HTMLBodyElement;
validHTMLTags  =/^(?:a|abbr|acronym|address|applet|area|article|aside|audio|b|base|basefont|bdi|bdo|bgsound|big|blink|blockquote|body|br|button|canvas|caption|center|cite|code|col|colgroup|data|datalist|dd|del|details|dfn|dir|div|dl|dt|em|embed|fieldset|figcaption|figure|font|footer|form|frame|frameset|h1|h2|h3|h4|h5|h6|head|header|hgroup|hr|html|i|iframe|img|input|ins|isindex|kbd|keygen|label|legend|li|link|listing|main|map|mark|marquee|menu|menuitem|meta|meter|nav|nobr|noframes|noscript|object|ol|optgroup|option|output|p|param|plaintext|pre|progress|q|rp|rt|ruby|s|samp|script|section|select|small|source|spacer|span|strike|strong|style|sub|summary|sup|table|tbody|td|textarea|tfoot|th|thead|time|title|tr|track|tt|u|ul|var|video|wbr|xmp)$/i;

function sanitize(txt) {
var // This regex normalises anything between quotes
    normaliseQuotes = /=(["'])(?=[^\1]*[<>])[^\1]*\1/g,
    normaliseFn = function ($0, q, sym) { 
        return $0.replace(/</g, '&lt;').replace(/>/g, '&gt;'); 
    },
    replaceInvalid = function ($0, tag, off, txt) {
        var 
            // Is it a valid tag?
            invalidTag = protos && 
                document.createElement(tag) instanceof HTMLUnknownElement
             || !validHTMLTags.test(tag),
            isComplete = txt.slice(off+1).search(/^[^<]+>/) > -1;

        return invalidTag || !isComplete ? '&lt;' + tag : $0;
    };

txt = txt.replace(normaliseQuotes, normaliseFn)
         .replace(/<(\w+)/g, replaceInvalid);

var tmp = document.createElement("DIV");
tmp.innerHTML = txt;

return "textContent" in tmp ? tmp.textContent : tmp.innerHTML;
}

HTML:

Enter some HTML here, invalid tags aren't removed:<br>


输出:

【讨论】:

    【解决方案2】:

    JS:

    string.replace(/(<([^>]+)>)/ig,"");
    

    Jquery(可能更慢):

    string.text();
    

    【讨论】:

    【解决方案3】:

    你是说这个吗?

    var yourtext = '<html><head><meta http-equiv="Content-Type" content="text/html;'+
    '> charset=utf-8"><meta name="Generator" content="Microsoft Exchange'+
    '> Server"><!-- converted from rtf --><style><!-- .EmailQuote {'+
    '> margin-left: 1pt; padding-left: 4pt; border-left: #800000 2px solid; }'+
    '> --></style></head><body><font face="Calibri" size="2"><span style="font-size:11pt;">      '+                   '<div>This is the body and text of the'+
    '> meeting.</div><div>&nbsp;</div></span></font></body></html>';
    
    var textWithoutHtml = $(yourtext).children().text();
    

    【讨论】:

    • 成功了!这个实际上删除了所有的 HTML
    • @user2816352 然后标记它。
    【解决方案4】:

    使用 jquery:

    $(yourtext).text();
    

    【讨论】:

      【解决方案5】:
      1. 使用 AJAX 或您正在使用的任何方式获取服务器响应。
      2. 在标签内存储服务器响应。
      3. 使用 javascript 或 .text() 使用 jQuery 获取该 div 的外部文本。

      请参阅工作示例 http//jsfiddle.net/imdadhusen/Hs4Q6/7/

      【讨论】:

        猜你喜欢
        • 2016-11-02
        • 2013-04-10
        • 2010-11-17
        • 2013-03-27
        • 2014-11-26
        • 2018-01-31
        • 2013-12-13
        • 1970-01-01
        相关资源
        最近更新 更多