私はこれで髪を失いました。メインが内部に持つタグの数に応じて、動的配列を作成したいと考えています。私を助けてください!
ここに私の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>
ここに私の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]);