[英]JQuery Selector for multiple elements of one ancestor
I have a DOM structure similar to this: 我有一个类似于以下的DOM结构:
<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? 如何选择祖先的某些后代?
Something like: 就像是:
$("#ans1 #in1, #ans1 #in2")
If you replace your 'id's with classes (since ids should be unique), then, 如果您将“ id”替换为类(因为ID应该是唯一的),那么,
<div id="ans1">
<input class="in1" />
<input class="in2" />
</div>
<div id="ans2">
<input class="in1" />
<input class="in2" />
</div>
Then, to select all the descendants of id=ans1 having class="in1", you go like, 然后,要选择id = ans1且具有class =“ in1”的所有后代,您会像这样,
$('#ans1 .in1')
This will return an array of all the .in1 class elements inside id=ans1 element 这将返回id = ans1元素内所有.in1类元素的数组
You can use the children function 您可以使用儿童功能
$("#ans1").children("#in1, #in2")
You should use unique ids thought the DOM, use classes to specify elements that are the same in nature. 您应该使用DOM唯一的ID,使用类来指定本质上相同的元素。
change your children to have same class of in1 将您的孩子换成同等级的in1
$("#ans1 > .in1")
Will select all direct descendants of ans1 with class of in1. 将选择ins类的ans1的所有直接后代。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.