计算div标签中img标签的数量 [英] Count number of img tags inside a div tag

查看:426
本文介绍了计算div标签中img标签的数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码是这样的.

<div id="some_id">
    <img src="some_image.png">
    <img src="some_image.png">
    <div class="another_div"></div>
    <div class="another_div"></div>
</div>

我想计算该div元素中的img标签的数量.

I want to count number of img tags inside that div element.

我从关于stackoverflow的类似问题中发现了这一点,该问题返回所有子代的计数.

I found this from a similar question on stackoverflow which returns count of all the children.

var count = $("#some_id").children().length;

如何修改此代码或使用其他功能来计算div中img标签的数量?

How do I modify this code or use some other function to count the number of img tags inside the div?

推荐答案

在#some_div中计数img:

Count img inside #some_div:

 $("#some_id img").length

如果您只想要直系子女,而不是所有后代:

If you want only the direct children, not all descendants:

$("#some_id > img").length

这篇关于计算div标签中img标签的数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆