$('.myclass') 仅当在此 id 下时
我有 2 个 div id #dfirst
和 #dfirst
,每个都有 .clickme
类的元素。我想选择仅位于 #dfirst
下的 .clickme
,而不是 #dsecond
下的 .clickme
。 请注意,嵌套级别有些不可预测,并且可能与我在这里得到的不同,因此我正在寻找在嵌套方面灵活的东西。那么jquery高手们,可以吗?
<div id="dfirst">
<div>
<div>
<div class="clickme" ></div>
<div class="clickme" ></div>
<div class="clickme" ></div>
<div>
<div/>
</div>
<div id="dsecond">
<div>
<div>
<div>
<div>
<div class="clickme" ></div>
<div class="clickme" ></div>
<div class="clickme" ></div>
<div>
<div>
<div>
</div>
I have 2 div ids #dfirst
and #dfirst
each has elements of class .clickme
. I want to select the .clickme
that are under only #dfirst
but not the ones under #dsecond
. Note that the level of nesting is somewhat unpredictable and may be different from what I've got here, so I'm looking for something that's flexible in terms of nesting. So jquery gurus, can it be done?
<div id="dfirst">
<div>
<div>
<div class="clickme" ></div>
<div class="clickme" ></div>
<div class="clickme" ></div>
<div>
<div/>
</div>
<div id="dsecond">
<div>
<div>
<div>
<div>
<div class="clickme" ></div>
<div class="clickme" ></div>
<div class="clickme" ></div>
<div>
<div>
<div>
</div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
只需使用一个空格(后代选择器),如下所示:
它将找到
元素,与#dfirst
下任何级别的 .clickme$("#dfirst").find(".clickme")
相同。Just use a space (the descendant selector), like this:
It'll find
.clickme
elements at any level beneath#dfirst
, the same as$("#dfirst").find(".clickme")
would.空格字符表示“后代”,只要
.clickme
在#dfirst
内部,嵌套多少层都没有关系。这与以下内容不同,后者表示仅直接在
#dfirst
中找到的.clickme
。A space character means "descendant", it doesn't matter how many levels of nesting as long as
.clickme
is inside#dfirst
.This is different from the following, which means only
.clickme
that is found directly within#dfirst
.$("#first .clickme")
将为您服务。JQuery 的选择器 CheatSheet 是任何使用它的 Web 开发人员的必备参考。
这是 DZone 的版本
$("#first .clickme")
will serves you.The JQuery's Selector CheatSheet is a must have reference for any web developer using it.
Here is a version by DZone