JQuery text()只删除HTML

时间:2015-07-03 12:35:55

标签: javascript jquery html

假设我有一个包含表格和其他元素的div。有没有办法让$ myElement.text()返回制表符分隔数据,就像我选择一个表并将其复制到剪贴板时一样?现在所有它只是删除标签并返回文本,包括空白的负载,这是相当无用的。

var $email = $('#printPage').clone().appendTo(document.body);
$email.find('.noprint, .printview').remove();
$email.find('.addressPanel').css({'width':'49%', 'display':'inline-block'});
$email.find('.dynamicTable table').css({'width':'100%'});
var html = $email.html().replace(/<!--.*?-->/g, '');
var text = $email.text(); //this gives me a huge amount of whitespace

2 个答案:

答案 0 :(得分:1)

$.fn.getDelimitedText = function(){
    var str = '';
    $(this).find('tr').each(function(trIdx,tr){
        $(tr).find('td').each(function(tdIdx, td){
            if(tdIdx > 0){
                str += '\t';
            }
            str += $(td).text();
        });
        str += '\n';
    });
    return str;
};

var x = $('table').getDelimitedText();

http://jsfiddle.net/daveSalomon/odh15d1s/2/

如果你想获得一个元素中的所有文本,你需要选择该元素的所有子元素,但是以稍微不同的方式处理任何表格....

var str = '';
$('#foo > *').each(function(idx,el){
    if(el.tagName == 'TABLE'){
        str += $(el).getDelimitedText();
    } else {
        str += $(el).text();
    }
    str += '\n';
});

console.log(str);

http://jsfiddle.net/daveSalomon/odh15d1s/4/

答案 1 :(得分:0)

好的,这就是我最终想出来的。可以添加的另一件事是用其equivelant值替换所有HTML实体(lt;&gt;等)。它可能会删除更多的换行符,直接从屏幕上复制它,但这就是我需要的。

$.fn.getDelimitedText = function(){
    var pres = [];
    var str = '';

    var checkSpace = /[ \r\n]+/gmi;
    var checkTags = /<(?!pre[ >])([^ \/>]*)(?:"[^"]*?"|[^">])*>/gmi;
    var checkLineBreaks = /\r?\n\r?\n/gmi;

    //Remove all comments immediately so they don't interfere.
    //Use alternation to catch line breaks also.
    var test = $(this).html().replace(/<!--(?:.|\n)*?-->/gmi, '');

    var count = 0;

    //replace all pre elements with a placeholder and store the contents 
    //in an array so they don't get modified.
    test.replace(/<(pre)(?: (?:"[^"]*?"|[^">])*)?>([\s\S]*?)<\/\1>/gmi, 
        function(match, tag, body, offset, input){
            pres.push(body);
            return '<pre ' + (pres.length - 1) + '/>';

        });


    function doTableRegex(outer, index){
        var lastPosition = 0;
        var table = {};

        //tbody, thead, tfoot. Don't assign the return to anything. We get that from the table var.
        outer.replace(/<(tbody|thead|tfoot)(?: (?:"[^"]*"|[^">])*)?>([\s\S]*?)<\/\1>/gmi, function(match, tag, body){
            //replace td and th elements
            body = body.replace(/<(th|td)(?: (?:"[^"]*"|[^">])*)?>([\s\S]*?)<\/\1>/gmi, function(match, tag, body1){
                return body1.replace(checkTags, '').replace(checkSpace, ' ') + "\t";
            });
            //replace tr elements
            body = body.replace(/<(tr)(?: (?:"[^"]*"|[^">])*)?>([\s\S]*?)<\/\1>/gmi, function(match, tag, body1){
                return body1.replace(checkTags, '').replace(checkSpace, ' ') + "\r\n";
            });
            //Multiple tbody elements are allowed in a table.
            //We'll allow for multiples of everything.
            if(table[tag]) table[tag] += "\r\n" + body;
            else table[tag] = body;
            //return nothing.
            return '';
        });

        //put the table sections in the correct order.
        return (table.thead || "") + "\r\n" + (table.tbody || "") + "\r\n" + (table.tfoot || "") + "\r\n";
    }
    //Replace all tables
    return test.replace(/<(table)(?: (?:"[^"]*"|[^">])*)?>([\s\S]*?)<\/\1>/gmi, doTableRegex)
    //replace br & hr tags with line breaks
    .replace(/<br ?\/?>|<hr ?\/?>/g, "\r\n")
    //remove all tags besides pre tags
    .replace(checkTags, '')
    //empty all lines of spaces
    .replace(/^ +$/gmi, '')
    //remove all empty lines
    .replace(/(?:\r?\n){2,}/gmi, "\r\n")
    //replace all double ( and more ) spaces with single spaces.
    .replace(/ +/gmi, ' ')
    //Add the contents of pre elements back in.
    .replace(/<pre(?: (?:id="([^"])"|"[^"]*"|[^">])*)?>/gmi, function(match, id){
        return "\r\n" + pres[parseInt(id)] + "\r\n";
    });

};