2009-08-27 12 views
0

テーブルがあり、各行からすべてのセルを取得するためにjqueryを使用します。これらのセルの中にはテキストとイメージがあります。javascript/jqueryを使用してテーブルからhtml imgを削除する方法

私はtdセル(私はこれをした)をつかみ、メモリ内のこれらのセルからイメージを削除したい(実際のテーブルから削除したくない)。

ので、私のセルは、私はjQueryを使ってこれを取得し、私は戻って今私がイメージタグを削除する方法がわからないのDOMを取得し、この

<td>hi their <img...... /> </td>ようになります。

ので、私はここに

var hold = // something to remove the img 

alert(hold) produced: "hi their". 

編集

をしたいが、私は

<table id="tbl"> 
    <thead> 
    <th> checkbox </th> 
    <th>header 1 </th> 
    <th>header 2 </th> 
    </thead> 
    <tfoot> 
    </tfoot> 
    <tbody> 
    <tr> 
     <td>checkbox </td> 
     <td>hi there woot</td> 
     <td>still no image </td> 
    </tr> 
    <tr> 
     <td>checkbox </td> 
     <td>a image is coming </td> 
     <td> here is the image <img alt="" src="" class="imgClass" </td> 
    </tr> 
    </tbody> 
</table> 



var rowCells = $('#tbl :checked').parents('tr td'); 

var header2 = $(rowCells[2]).html() // grabs "still no image" and "here is the image <img ....."/> 

持っているものですので、私は働いていたとして置き換えるとかとnothihgのように試してみました。

私はHEADER2変数がちょうど "ここiamgeがある"

答えて

1

更新の答えが欲しい:

var header2 = $(rowCells[2]).html().replace(/<img[^>]+>/g, ''); 
var header2 = $(rowCells[2]).html().find('img').remove() 

未テスト:クローンを使用して

var hold = $('td:first').html().replace(/<img[^>]+>/g, ''); 
alert(hold); 

代替方法:

var hold = $('td:first').clone().find('img').remove().end() 
0

はこれを試してみてください:

var tdContent = $('td').clone(); 
tdContent.children('img').remove(); 
alert(tdContent.html()); 

これはどのあなたがimgタグのストリップをすることができますメモリ内のDOM要素のクローンを作成します。

関連する問題