get the elements number inside parent div jquery

2019-03-01 13:58发布

问题:

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?

回答1:

$("#preview_images_gallery > img").size()

on second thoughts

  $("#preview_images_gallery > img").length


回答2:

$('#preview_images_gallery > img').length


回答3:

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



回答4:

alert($("#preview_images_gallery img").length);//return numbers of images inside preview_images_gallery 


回答5:

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);


标签: jquery size