나는 이걸로 내 머리카락을 잃어 버렸습니다. 내 메인에있는 태그의 수에 따라 동적 배열을 만들고 싶습니다. 도와주세요! 여기 푸시 방법이 작동하지 않습니다.
내 JS 내 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>
입니다
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해야한다이 때 컬렉션에 12 요소를 참조하려고하는 것을 알 수 있습니다 : 만 얻을 요소에'container.children'를 사용하여 . 'childNodes'는 엘리먼트와 텍스트 노드를 얻습니다. 그러면 수동으로 필터링해야합니다. –