get the elements number inside parent div jquery

2019-03-01 14:18发布

I have a div that contain many images:

<div id="#preview_images_gallery">
 <img class="image_url pager" src="image1.jpg">
 <img class="image_url pager" src="image2.jpg"> 
 <img class="image_url pager" src="image3.jpg">
 .
 .
 .
</div>

how I can know the number of images contained in this div with jquery?

标签: jquery size
5条回答
Animai°情兽
2楼-- · 2019-03-01 14:27
$('#preview_images_gallery > img').length
查看更多
迷人小祖宗
3楼-- · 2019-03-01 14:35
$("#preview_images_gallery > img").size()

on second thoughts

  $("#preview_images_gallery > img").length
查看更多
霸刀☆藐视天下
4楼-- · 2019-03-01 14:39
var imagecount = $('#preview_images_gallery img').length

the length property of a jQuery collection.

Note : you should change your div id from #preview_images_gallery to preview_images_gallery

查看更多
放我归山
5楼-- · 2019-03-01 14:47
alert($("#preview_images_gallery img").length);//return numbers of images inside preview_images_gallery 
查看更多
虎瘦雄心在
6楼-- · 2019-03-01 14:51

This is how you can count the img, in plain javascript way :

var get=document.getElementById('preview_images_gallery');
var allget=get.getElementsByTagName('img');
alert(allget.length);
查看更多
登录 后发表回答