Javascript 'range' equivilent
So I have a div with a number if images in it and I want to count those images and display and the count thus:
If there are 5 images:
1,2,3,4,5
If this were python or PHP, I would simply g开发者_运维百科et the number of images and then iterate through a 'foreach' loop. However I can't figure how to do this with Javascript.
I am counting the number of images this way:
<script>
$(document.body).ready(function() {
var n = $("#gallery img").length;
});
</script>
Any ideas how I can accomplish this using javascript?
<script>
$(document.body).ready(function() {
var n = $("#gallery img").length;
var $elementToAppend = $("#elementToAppend");
if (n > 0)
$elementToAppend.append("0");
for(var i=1; i<n; i++)
$elementToAppend.append("," + i);
});
</script>
精彩评论