开发者

Javascript 'range' equivilent

开发者 https://www.devze.com 2023-02-26 12:31 出处:网络
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:

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>
0

精彩评论

暂无评论...
验证码 换一张
取 消