J'ai perdu mes cheveux sur celui-ci. Je veux créer un tableau dynamique en fonction du nombre de tags que mon principal a à l'intérieur. Aidez-moi, s'il vous plaît!méthode push ne fonctionne pas
Voici mon HTML
<div id="image-slider">
<a href="http://www.google.com"><img src="images/slider1.png"></a>
<a href="http://www.phpacademy.org"><img src="images/slider2.png"></a>
<a href="http://www.ign.com"><img src="images/slider3.png"></a>
<a href="http://www.w3schools.com"><img src="images/slider4.png"></a>
<a href="http://www.nfl.com"><img src="images/slider5.png"></a>
</div>
Voici mon JS
container = document.getElementById('image-slider');
containerLength = container.childNodes.length;
images = new Array();
for(var i=0;i<=containerLength;i++){
if (container.childNodes[i].nodeName == 'A') {
alert(container.childNodes[i]);// here I get the desired value
images.push(container.childNodes[i]);
//this doesn't work even if i use images.push('Hello');
}
}
alert(images[0]); //doesn't even pops out
// if I do it like this.... i get the desired result! I don't know why! but this is not what i want
images.push(container.childNodes[1]);
alert(images[0]);
Protip: Utilisez 'container.children' pour seulement obtenir des éléments . 'childNodes' obtient des éléments et des nœuds de texte, que vous devez ensuite filtrer manuellement. –