2015-07-03 82 views
-1

說我有一個包含表格和其他元素的div。有沒有辦法讓$ myElement.text()返回製表符分隔的數據,就像我選擇一個表並將其複製到剪貼板時一樣?現在所有它只是刪除標籤並返回文本,包括大量的空白,這是無用的。JQuery文本()僅刪除HTML

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

我們可以得到一個JSFiddle代碼來更好地理解您的問題嗎? –

+0

$ myTable.html()? –

+5

'.text()'和'.html()'都不會做你想要的。您必須編寫自己的代碼來遍歷錶行,以便爲每個表生成一個製表符分隔的表示形式。 – Pointy

回答

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/

+0

我應該拋出圍繞它的文字。你會怎麼做? –

+0

_what_有什麼文字?這是遍歷表中的每一行,並獲取每個單元格內的文本? –

+0

抱歉,桌子周圍。 –

0

好吧,這裏是我終於想出了。另外一個可以添加的東西是將所有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"; 
    }); 

};