2010-06-27 49 views
0

我有一個像不能訪問子元素 - jQuery的

<div class="a"> 
    <div class="b"> 
     something 
    </div> 

    <div class="c"> 
     <div class="subC"> 
      i want to access 
     </div> 
    </div> 
</div> 

和jQuery一個HTML像

$('.a').hover(function(){ 
    $(this).children('.subC').fadeOut(); 
}) 

我要訪問的類 「subC的」,但上面不工作。

我也嘗試

$('.a').hover(function(){ 
    $(this).children('.c .subC').fadeOut(); 
}) 

,但是這也不能正常工作!

這是什麼問題的解決方案!我做錯了什麼?請幫助

回答

0

使用.find('selector')找到深的孩子

0

正如羅布說,使用.find找到深元素。

$('.a').hover(function() 
    { 
     $(this).find('.c .subC').fadeOut(); 
    }); 

,如果你想使用.children,寫

$('.a').hover(function(){ 
    $(this).children('.c').children('.subC').fadeOut(); 
}) 
1

當一個jQuery瓶蓋內,this是指由先前的jQuery操作返回的jQuery對象:

$('.a').hover(function() { 
    // 'this' is a jQuery object containing the result of $('.a') 
}) 

使用this在閉包內設置查詢當前jQuery對象的範圍:

$('.a').hover(function() { 
    $('.subC', this).fadeOut(); 
})