0

inputユーザーのhtmlデータを抽出して、objectまたはに保存しようとしていますarray

のソースはhtml次のようになります

begin <em>texts</em>
    description texts here

<table>
   <tr><td>cell 1<td><td>cell2</td></tr>
   <tr><td>cell 3<td><td>cel4</td></tr>
</table>

<em>second<em> part texts

<table>
   <tr><td>cell 5<td><td>cell6</td></tr>
   <tr><td>cell 7<td><td>cel8</td></tr>
</table>

以下のものを収納できる必要があります。

テーブル外のすべてのテキストとテーブル内のセル データ。それらは順番にある必要があります。

私が試してみました

var data = [];
sourceHtml.contents().each(function(){
    if($(this).is('table')){
          var table = $(this)
          var rows = 'rows:' + $('tr', table).length;
          var column ='column:' + $('td', table).length / $('tr', table).length;
          //i need to get the table columns and rows counts
          data.push(rows)
          data.push(column)

          table.find('td').each(function(){
              data.push($(this).text().trim());
          })
        }else{
          data.push($(this).text().trim());
        }
 })

上記は、のすべてのテキストのみを提供しますが、テキストのsourceHtmlすべてを失いますmarkuphtmlデータのテキストを取得しながらマークアップを維持する方法はありますか? 助けてくれてどうもありがとう!

4

2 に答える 2

1

$(this).text().trim()使用する代わりに$(this).html()

お役に立てれば

于 2013-07-19T20:09:40.957 に答える
1

テキスト ノード コンテンツの代わりに要素の HTML マークアップ コンテンツを取得するには、.html()代わりに使用します。.text()

http://api.jquery.com/html/

于 2013-07-19T20:10:02.427 に答える