2012-01-30 79 views
0

我正在運行一些jQuery來添加/刪除當前類。它適用於兄弟姐妹,但不適用於兄弟姐妹的孩子。任何人有什麼好主意?爲什麼jQuery不刪除類

$('#rotator_buttons td').click(function(){ 
    if($(this).hasClass('current')) { 
     $(this).removeClass('current'); 
    } 
    else { 
     $(this).addClass('current').siblings().removeClass('current'); 
     $(this).siblings().children('a').removeClass('current'); 
     $(this).children('a').addClass('current'); 

    } 
    return false; 
}); 

HTML

<table id="rotator_buttons"> 
<tbody> 
<tr> 
    <td id="pause-play"><a href="#"><!-- --></a></td> 
    <td><a href="#" id="1" target="_self" onclick="javascript:increment_slideshow(0)">Button 1</a></td> 
    <td><a href="#" id="2" target="_self" onclick="javascript:increment_slideshow(0)">Button 2</a></td> 
    <td><a href="#" id="3" target="_self" onclick="javascript:increment_slideshow(0)">Button 3</a></td> 
    <td><a href="#" id="4" target="_self" onclick="javascript:increment_slideshow(0)">Button 4</a></td> 
</tr> 

+2

他們是真正的孩子,或者是他們嵌套更深?當我們看不到DOM時,幾乎不可能回答DOM選擇問題。 – 2012-01-31 00:02:59

+0

根據類'current'的使用位置,你可能只想做'$(this).parent()。find('。current')。removeClass('current')'。 – 2012-01-31 00:08:19

回答

0
$('#rotator_buttons td').click(function(){ 
    if($(this).hasClass('current')) { 
     $(this).removeClass('current'); 
    } 
    else { 
     $(this).children('a').addClass('current'); 
     $(this).siblings().each(function(){ 
      $(this).removeClass('current'); 
      $(this).children('a').removeClass('current'); 
     }); 
    } 
    return false; 
}); 
+0

這工作...謝謝! – 2012-01-31 16:05:31