jQuery选择器,用于一个祖先的多个元素 [英] JQuery Selector for multiple elements of one ancestor

查看:120
本文介绍了jQuery选择器,用于一个祖先的多个元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个与此类似的DOM结构:

I have a DOM structure similar to this:

<div id="ans1">
    <input id="in1" />
    <input id="in2" />
</div>
<div id="ans2">
    <input id="in1" />
    <input id="in2" />
</div>

如何选择某个祖先的后代?

How can I select some of the descendants of an ancestor?

类似的东西:

$("#ans1 #in1, #ans1 #in2")

推荐答案

您可以使用children函数

You can use the children function

$("#ans1").children("#in1, #in2")

您应该使用与DOM相同的唯一ID,使用类来指定本质上相同的元素.

You should use unique ids thought the DOM, use classes to specify elements that are the same in nature.

将您的孩子换成同等级的in1

change your children to have same class of in1

$("#ans1 > .in1")

将选择ins类的ans1的所有直接后代.

Will select all direct descendants of ans1 with class of in1.

这篇关于jQuery选择器,用于一个祖先的多个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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