2009-08-27 8 views
0

J'ai une table et j'utilise jquery pour obtenir toutes les cellules de chaque rangée. Certaines de ces cellules contiennent du texte et une image.comment supprimer un html img d'une table avec javascript/jquery

Je veux attraper la cellule td (j'ai fait ceci) puis enlever l'image de ces cellules en mémoire (je ne veux pas les enlever de la table réelle).

donc ma cellule ressemblerait à ceci

<td>hi their <img...... /> </td> Je prends cela avec jquery et je reçois le Royaume en arrière maintenant, je ne sais pas comment supprimer la balise d'image.

donc je veux

var hold = // something to remove the img 

alert(hold) produced: "hi their". 

Modifier

voici ce que j'ai

<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 ....."/> 

donc j'ai essayé comme remplacer et d'autres choses et nothihg comme travaillé.

Je veux variables header2 d'avoir juste "voici le iamge"

Répondre

1

des réponses à jour:

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

Untested:

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

manière alternative utilisant des clones:

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

Essayez ceci:

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

Cela clone l'élément DOM dans la mémoire que vous pouvez ensuite bande de img tags.

Questions connexes