2012-03-28 48 views
0

我想要的類中去除的L1標籤的頁面加載...如何在jquery父母父類中訪問?

HTML:

<ul class="NewsArchive"> 
       <li class="off"> 
        <a href="#" class="year">T's Blog</a>     
         <ul class="NewsMonths" style="display: block; "> 
            <li class="off"> 
            <a href="#" class="month">July 2011</a>    
             <ul class="BlogsArt" style="display: block; "> 
               <li class="turnedon"> 
                What's in a surname? 
               </li> 
            </ul> 
           </li> 
</ul> 
</li> 
<li class="off"> 
       <a href="#" class="year">Steve's Blogs</a>     
        <ul class="NewsMonths"> 
           <li class="off"> 
           <a href="#" class="month">February 2012</a>    
            <ul class="BlogsArt"> 
              <li> 
               A bright note 
              </li> 
           </ul> 
          </li> 
        </ul> 
      </li> 
</ul> 

我想是對於具有其父父李「turnedon」李班級「關閉」被拆除。所以頂級課。如果這是有道理的?

回答

0

這應該可以做到。

$(function() 
{ 
    $('li.turnedon').closest('li.off').removeClass('off'); 
}); 

這將刪除父類off,如果它包含它。

$(function() 
{ 
    $('li.turnedon').parents('li.off').removeClass('off'); 
}); 
+0

優秀的感謝! – Beginner 2012-03-28 16:00:37

0

只需使用$.parent()$.removeClass - 簡單......如果你想刪除所有的家長(遞歸),你可以使用類似

var parent = $.parent(); // this needs to be called for the correct li object 
do { 
    parent.removeClass('off') 
    parent = parent.parent(); 
} while (parent)