JQuery - 如何检测给定div中给定类的多少个div? [英] JQuery - how do i detect how many divs of a given class exist within a given div?

查看:61
本文介绍了JQuery - 如何检测给定div中给定类的多少个div?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的div:

I have a div like this:

<div id="x" name="x" class="x" style=""></div>

包含在这个div中我有几个div像这样:

and contained within this div I have several divs like this:

<div id="y_1" name="y_1" class="y" style=""></div>
<div id="y_2" name="y_2" class="y" style=""></div>
<div id="y_3" name="y_3" class="y" style=""></div>

等。

问题1:
如何检测容器div(class =x)中包含多少个这样的div(class =y)? - (只是一个提示(),例如数字)。

QUESTION 1: How do I detect how many of these divs (class="y") are contained within the container div (class="x")? - (just an alert("") with the number, for example).

问题2:
如何对这些y- div(class =y),例如可能使用$('。y')。html(Y)将字母Y放入所有y-div中的函数。 ,例如??

QUESTION 2: How do I do something to each of these y-divs (class="y") such as a function that might place the letter "Y" into all of the y-divs using $('.y').html("Y"); , for example??

任何帮助表示赞赏的人....

Any help appreciated guys....

推荐答案

$('#x div.y').length; // number of class y elements under element x
$('#x div.y').html('Y'); // run a jQuery method on the y elements

查看API:

  • descendant selector
  • length property

这篇关于JQuery - 如何检测给定div中给定类的多少个div?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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