RemoveChild capricieux

Résolu
armata Messages postés 9 Date d'inscription vendredi 14 novembre 2003 Statut Membre Dernière intervention 8 avril 2009 - 8 avril 2009 à 16:29
armata Messages postés 9 Date d'inscription vendredi 14 novembre 2003 Statut Membre Dernière intervention 8 avril 2009 - 8 avril 2009 à 18:09
<meta http-equiv= "Content-Type" content="text/html; charset=utf-8" />
<meta name="ProgId" content="Word.Document" />
<meta name="Generator" content="Microsoft Word 11" />
<meta name="Originator" content="Microsoft Word 11" />
<link rel="File-List" href="file:///C:%5CDOCUME%7E1%5CArMaTa%5CLOCALS%7E1%5CTemp%5Cmsohtml1%5C01%5Cclip_filelist.xml" />
<!--[if gte mso 9]><xml>
<w:WordDocument>
<w:View>Normal</w:View>
<w:Zoom>0</w:Zoom>
<w:HyphenationZone>21</w:HyphenationZone>
<w:PunctuationKerning/>
<w:ValidateAgainstSchemas/>
<w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid>
<w:IgnoreMixedContent>false</w:IgnoreMixedContent>
<w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText>
<w:Compatibility>
<w:BreakWrappedTables/>
<w:SnapToGridInCell/>
<w:WrapTextWithPunct/>
<w:UseAsianBreakRules/>
<w:DontGrowAutofit/>
<w:UseFELayout/>
</w:Compatibility>
<w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel>
</w:WordDocument>
</xml><![endif]-->
<!--[if gte mso 9]><xml>
<w:LatentStyles DefLockedState="false" LatentStyleCount="156">
</w:LatentStyles>
</xml><![endif]-->
<style>
<!--
/* Font Definitions */
@font-face
{font-family:SimSun;
panose-1:2 1 6 0 3 1 1 1 1 1;
mso-font-alt:宋体;
mso-font-charset:134;
mso-generic-font-family:auto;
mso-font-pitch:variable;
mso-font-signature:3 135135232 16 0 262145 0;}
@font-face
{font-family:"\@SimSun";
panose-1:2 1 6 0 3 1 1 1 1 1;
mso-font-charset:134;
mso-generic-font-family:auto;
mso-font-pitch:variable;
mso-font-signature:3 135135232 16 0 262145 0;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{mso-style-parent:"";
margin:0cm;
margin-bottom:.0001pt;
mso-pagination:widow-orphan;
font-size:12.0pt;
font-family:"Times New Roman";
mso-fareast-font-family:SimSun;}
@page Section1
{size:612.0pt 792.0pt;
margin:70.85pt 70.85pt 70.85pt 70.85pt;
mso-header-margin:36.0pt;
mso-footer-margin:36.0pt;
mso-paper-source:0;}
div.Section1
{page:Section1;}
-->
</style>
<!--[if gte mso 10]>
<style>
/* Style Definitions */
table.MsoNormalTable
{mso-style-name:"Tableau Normal";
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-noshow:yes;
mso-style-parent:"";
mso-padding-alt:0cm 5.4pt 0cm 5.4pt;
mso-para-margin:0cm;
mso-para-margin-bottom:.0001pt;
mso-pagination:widow-orphan;
font-size:10.0pt;
font-family:"Times New Roman";
mso-fareast-font-family:"Times New Roman";
mso-ansi-language:#0400;
mso-fareast-language:#0400;
mso-bidi-language:#0400;}
</style>
<![endif]-->
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="ProgId" content="Word.Document" />
<meta name="Generator" content="Microsoft Word 11" />
<meta name="Originator" content="Microsoft Word 11" />
<link rel="File-List" href="file:///C:%5CDOCUME%7E1%5CArMaTa%5CLOCALS%7E1%5CTemp%5Cmsohtml1%5C01%5Cclip_filelist.xml" />
<!--[if gte mso 9]><xml>
<w:WordDocument>
<w:View>Normal</w:View>
<w:Zoom>0</w:Zoom>
<w:HyphenationZone>21</w:HyphenationZone>
<w:PunctuationKerning/>
<w:ValidateAgainstSchemas/>
<w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid>
<w:IgnoreMixedContent>false</w:IgnoreMixedContent>
<w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText>
<w:Compatibility>
<w:BreakWrappedTables/>
<w:SnapToGridInCell/>
<w:WrapTextWithPunct/>
<w:UseAsianBreakRules/>
<w:DontGrowAutofit/>
<w:UseFELayout/>
</w:Compatibility>
<w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel>
</w:WordDocument>
</xml><![endif]-->
<!--[if gte mso 9]><xml>
<w:LatentStyles DefLockedState="false" LatentStyleCount="156">
</w:LatentStyles>
</xml><![endif]-->
<style>
<!--
/* Font Definitions */
@font-face
{font-family:SimSun;
panose-1:2 1 6 0 3 1 1 1 1 1;
mso-font-alt:宋体;
mso-font-charset:134;
mso-generic-font-family:auto;
mso-font-pitch:variable;
mso-font-signature:3 135135232 16 0 262145 0;}
@font-face
{font-family:"\@SimSun";
panose-1:2 1 6 0 3 1 1 1 1 1;
mso-font-charset:134;
mso-generic-font-family:auto;
mso-font-pitch:variable;
mso-font-signature:3 135135232 16 0 262145 0;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{mso-style-parent:"";
margin:0cm;
margin-bottom:.0001pt;
mso-pagination:widow-orphan;
font-size:12.0pt;
font-family:"Times New Roman";
mso-fareast-font-family:SimSun;}
@page Section1
{size:612.0pt 792.0pt;
margin:70.85pt 70.85pt 70.85pt 70.85pt;
mso-header-margin:36.0pt;
mso-footer-margin:36.0pt;
mso-paper-source:0;}
div.Section1
{page:Section1;}
-->
</style>
<!--[if gte mso 10]>
<style>
/* Style Definitions */
table.MsoNormalTable
{mso-style-name:"Tableau Normal";
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-noshow:yes;
mso-style-parent:"";
mso-padding-alt:0cm 5.4pt 0cm 5.4pt;
mso-para-margin:0cm;
mso-para-margin-bottom:.0001pt;
mso-pagination:widow-orphan;
font-size:10.0pt;
font-family:"Times New Roman";
mso-fareast-font-family:"Times New Roman";
mso-ansi-language:#0400;
mso-fareast-language:#0400;
mso-bidi-language:#0400;}
</style>
<![endif]-->
Bonjour,


Alors voila j’ai un soucis avec un RemoveChild voici mon
code (enfin version simple) :



 













                                   blabla |






                                   blabla |






                                   blabla |






                                   blabla |
















 





Mon but est de supprimer toutes les tables qui ont un th de
classe ’classRm’ et pour cela je comptais faire :



 




var tab =document.getElementsByTagName('table');



for
(i=0;i<tab.length;i++){





var th=tab[i].getElementsByTagName('th');






            for (j=0;j<th.length;j++){






                        if
(th[j].className==" classRm "){






                                   tab[i].parentNode.removeChild(tab[i]);






                                   break;






                        }






            }





}






 





Sauf que ceci n’a aucun effet sur ma page web.



 




Pour voir mes tableaux disparaître il me faut faire :



 





tab[i] .parentNode.parentNode.removeChild(tab[i].parentNode);






 





ce qui me supprime mon div et donc mes autres tableaux aussi,
ce n’est donc pas ce que je souhaite.



 




Pourriez vous s’il vous plaît éclairer ma lanterne sur ceci ?






ARMATA

6 réponses

PetoleTeam Messages postés 3426 Date d'inscription lundi 26 décembre 2005 Statut Membre Dernière intervention 14 janvier 2011 17
8 avril 2009 à 18:03
SORRY
mal lu le
post...
...dans ce cas ton code est bon sauf que
tab[i].parentNode.removeChild(tab[i]);
on est d'accord cela
supprime la table donc quand tu continues la boucle ici
for( i= 0; i< tab.length;i++){
tab.length est décrémenté, donc il faut recalé ton i.

au final
function
Del_Table(){
  var tab = document.getElementsByTagName('table');
  for(
i=0; i< tab.length;i++){
    var th = tab[i].getElementsByTagName('th');

      for( j=0; j < th.length;j++){
        if (th[j].className ==
"classRm"){
          tab[i].parentNode.removeChild(tab[i]);

          i--; // replace l'index avant nouvel
incrementation
          break;
        }
     }
  }

}

;O)
3
Zobibol Messages postés 469 Date d'inscription mercredi 9 janvier 2002 Statut Membre Dernière intervention 20 février 2017 6
8 avril 2009 à 17:23
Bonjour, il semblerait qu'une petite erreur se soit glisser dans le code ;o)

var tab= document.getElementsByTagName('table');

for (i=0;i<tab.length;i++){

var th=tab[i].getElementsByTagName('th');

            for (j=0;j<th.length;j++){

                        if (th[j].className== " classRm " ){

                                   tab[i].parentNode.removeChild(tab[i]);

                                   break;

                        }

            }

}

il y a des espaces dans entre les guillemets et le nom de la classe ça devrait être :
var tab =document.getElementsByTagName('table');

for (i=0;i<tab.length;i++){

var th=tab[i].getElementsByTagName('th');

            for (j=0;j<th.length;j++){

                        if (th[j].className=="classRm"){

                                   tab[i].parentNode.removeChild(tab[i]);

                                   break;

                        }

            }

}

Et la magie du javascript s'accomplie ;o)
[o-_-o]
0
PetoleTeam Messages postés 3426 Date d'inscription lundi 26 décembre 2005 Statut Membre Dernière intervention 14 janvier 2011 17
8 avril 2009 à 17:43
Bonjour,
tu te trompes de parent
TABLE ->
TBODY -> TR -> TH, la TH est au même niveau qu'une TD

function Del_Table(){
  var tab =
document.getElementsByTagName('table');
  for( i=0; i< tab.length;i++){

    var th = tab[i].getElementsByTagName('th');
      for( j=0; j <
th.length;j++){
        if (th[j].className == "classRm"){

          tab[i].parentNode.removeChild(tab[i]);// Ta ligne a supprimer

          th[j].parentNode.removeChild(th[j]);   // on prend le parent de la TH
          break;

        }
     }
  }
}
;O)
0
armata Messages postés 9 Date d'inscription vendredi 14 novembre 2003 Statut Membre Dernière intervention 8 avril 2009
8 avril 2009 à 17:45
merci de ta réponse mais non malheureusement ce n'est qu'une faute de recopie, dans mon code ces espaces n'y sont pas donc une autres idée?

ARMATA
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
armata Messages postés 9 Date d'inscription vendredi 14 novembre 2003 Statut Membre Dernière intervention 8 avril 2009
8 avril 2009 à 17:49
Merci mais si je ne m'abuse ton code aura pour effet de supprimer les balises 'th' alors que mon but est de supprimer les tables entières qui contiennent une balise 'th'.
oui je sais j'en demande beaucoup

ARMATA
0
armata Messages postés 9 Date d'inscription vendredi 14 novembre 2003 Statut Membre Dernière intervention 8 avril 2009
8 avril 2009 à 18:09
Yesssss merci beaucoup de ton aide c'été bien mon problème. t'es un chef :p

ARMATA
0
Rejoignez-nous