开发者

$('.myclass') ONLY when under this id

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="cli开发者_如何学编程ckme" ></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>


Just use a space (the descendant selector), like this:

$("#dfirst .clickme")

It'll find .clickme elements at any level beneath #dfirst, the same as $("#dfirst").find(".clickme") would.


A space character means "descendant", it doesn't matter how many levels of nesting as long as .clickme is inside #dfirst.

$('#dfirst .clickme')

This is different from the following, which means only .clickme that is found directly within #dfirst.

$('#dfirst > .clickme')


$("#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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜