가 두 개 이상의 요소에 동일한 ID를 추가해서는 안 될 것입니다. 클래스 이름을 사용하십시오. document.getElemetsById
은 정의 된 함수가 아니며 하나의 요소를 반환해야하는 document.getElementById
("s"제외) 만 있습니다.
HTML :
<div class="stick"> </div>
<div class="stick"> </div>
JS :
<script type="text/javascript">
var divs = document.getElementsByClassName('stick');
for(var i = 0; i < divs.length; i++) {
var rand = 200 * Math.random();
divs[i].style.height= rand+'px';
}
</script>
이 수도 작동합니다.
<script type="text/javascript">
$('.stick').each(function(){ //iterates all elements having stick class
$(this).height(200 * Math.random()); //inside the callback the 'this' is the current html element. etc ...
});
</script>
jQuery를 홈페이지 : http://jquery.com/
빨리 당신의 웹 사이트에 포함 바로 CDN의 URL에서 jQuery를 가져,이 선이 jQuery를에 간단한 코드되기 때문에하지만, 자바 스크립트 코딩의 jQuery 프레임 워크를 사용하는 것이 좋습니다 , 머리 안쪽 :
<head>
...
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js" type="text/javascript"></script>
</head>