我一直在为这个脱发。我想根据我的 main 里面有多少标签来创建一个动态数组。请帮我!
这是我的 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]);