admin管理员组文章数量:1425875
So I have multiple divs with the same id 'stick'. I want to iterate through all these elements and give each a random height. here is what I tried:
<script type="text/javascript">
for(var i = 0; i < 47; i++) {
var rand = 200;
document.getElementsById('stick')[i].style.height= rand+'px';
}
</script>
This code doesn't work sadly. Am I missing something?
Thanks.
So I have multiple divs with the same id 'stick'. I want to iterate through all these elements and give each a random height. here is what I tried:
<script type="text/javascript">
for(var i = 0; i < 47; i++) {
var rand = 200;
document.getElementsById('stick')[i].style.height= rand+'px';
}
</script>
This code doesn't work sadly. Am I missing something?
Thanks.
Share Improve this question edited Mar 16, 2013 at 6:58 ThinkingStiff 65.4k30 gold badges147 silver badges241 bronze badges asked Mar 9, 2013 at 9:08 Duncan PalmerDuncan Palmer 2,91311 gold badges65 silver badges93 bronze badges3 Answers
Reset to default 5You should not add same id for more than one element.
Use class names. And document.getElemetsById
is not a defined function, only document.getElementById
(without "s") that should return one element.
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>
This may works. But I remend to use jQuery framework, for JavaScript coding, because these lines bee a simpler code in jQuery:
<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 homepage: http://jquery./
Fast including to your website, just import the jQuery from CDN url, inside the head:
<head>
...
<script src="//ajax.googleapis./ajax/libs/jquery/1.9.1/jquery.min.js" type="text/javascript"></script>
</head>
ID's should be pletely unique within the document. Change your ID's to unique values, and you can reference them individually. If you wish to act on a collection of elements, use the class
tag to link them together. To read up on CSS Classes
you can look at this documentation. This will help you understand the purpose of classes in HTML and CSS.
Your problem
getElementsByID()
doesn't work. getElementByID()
is the correct syntax.
So
document.getElementsById('stick')[i].style.height= rand+'px';
Will bee
document.getElementById('stick')[i].style.height= rand+'px';
You can't have multiple elements with the same id
. id
must be unique. You need to use class
and retrieve your elements using class name. You could use jquery to assist you: var elements = $('.stick')
本文标签: javascriptChange multiple divs of same id to different heightsStack Overflow
版权声明:本文标题:javascript - Change multiple divs of same id to different heights - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745429446a2658260.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论