2010-07-23 5 views
0

J'essaye d'analyser une donnée de réponse au format html. Les données contiennent plusieurs tables (pas de tables imbriquées). Je charge le html avec ajax et essaie de faire défiler les données avec jquery.Comment faire une boucle sur plusieurs tables avec jQuery dans un fichier html chargé avec ajax

Le problème est Aucun résultat quand je commence avec 'table' pour la boucle (j'ai plusieurs tables et n'ai aucun ID pour la table.) Quand j'utilise 'tr' Cela fonctionne bien dans Firefox et chrome mais pas dans IE.

Je voudrais savoir comment je peux parcourir ces tableaux. Voici le code que j'essayais.

<html> 
<head> 
<title>Html table tParser</title> 
    <script src="http://code.jquery.com/jquery-latest.min.js"></script> 
<script language="JavaScript"> 
$(document).ready(function(){ 
     $.ajax({ 
       url: "htmltables.html", 
       cache: false, 
       success: function(html) 
       { 
       //alert((html.length)); 
       //alert($(html).find('table').size()); 
       $(html).find('table').each(function(index) 
       { 
        document.write('<br />'+'<br />'+ 'Table:' + index + '<br />') 
        $(this).find('tr').each(function(index) { 
         document.write('<br />'+'<br />'+ 'Row :' + index + '<br />');      
         $(this).find('td').each(function(index) {              
          document.write($(this).text() + '<br />'); 
         }); //td 
        }); //tr  
       }); //table 
     } //success 
      }); // $.ajax(
    });//$(document).ready(function  

    </script> 
    </head> 
    <body> 
    </body> 
</html> 

/*------------------------------ */ 
/* Here is the sample html file I was trying to parse*/ 

<html> 
<head> 
<title></title> 
</head> 

<body> 
<table width="200" border="1"> 
    <tr> 
    <td>1</td> 
    <td>1</td> 
    <td>1</td> 
    </tr> 
    <tr> 
    <td>2</td> 
    <td>2</td> 
    <td>2</td> 
    </tr> 
    <tr> 
    <td>3</td> 
    <td>3</td> 
    <td>3</td> 
    </tr> 
</table> 
<table width="200" border="1"> 
    <tr> 
    <td>4</td> 
    <td>4</td> 
    <td>5</td> 
    </tr> 
    <tr> 
    <td>5</td> 
    <td>4</td> 
    <td>5</td> 
    </tr> 
    <tr> 
    <td>6</td> 
    <td>4</td> 
    <td>5</td> 
    </tr> 
</table> 

</body> 
</html> 
+0

beurk, formatez votre code! indentez-le 4 espaces s'il vous plaît! – Patricia

Répondre

0

peut-être essayer

$(html).wrap('<span>').parent().find('table') 
+0

Merci, Cela fonctionne bien dans Chrome et Firefox. IE affiche juste 'Table 0' – surendran

+0

Cela fonctionne dans IE maintenant, document.write était le problème. encore un autre problème, IE traiter la table dans l'ordre inverse, la dernière table d'abord? J'utilise, $ (html) .wrap ('') .parent(). Find ('table'). Chaque (fonction (index) { est-ce qu'il y a quelque chose qui ne va pas? – surendran

+0

Envelopper une table dans une travée? utilise un div. – David

1

Vous pouvez ajouter la table à un élément caché puis l'analyser:

var wrapper = $('<div>').hide().appendTo(document.body); 
// [...] 
success: function(html) { 
    wrapper.append(html); 
    var table = wrapper.find('table'); 
    alert(table.length); // should be at least 1 

    // parse the HTML here 

    wrapper.show(); // will reveal the html 
} 
+0

Merci, Cela fonctionne, avec un problème Cela fonctionne dans Chrome et Firefox. Excepté "wrapper.show()" IE il impression juste "Table0", comment puis-je faire ce travail dans IE aussi? Pourquoi "table" ne fonctionne pas sans ajout de DOM, mais "tr" fonctionne toujours? – surendran

+0

Cela fonctionne dans IE maintenant, document.write était le problème. – surendran

Questions connexes