get the elements number inside parent div jquery

时间秒杀一切 提交于 2019-12-02 11:31:26

问题


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


来源:https://stackoverflow.com/questions/9483670/get-the-elements-number-inside-parent-div-jquery

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!