2012-08-30 162 views
0

我碰到一个问题;我试图让所有其他DIV s关闭并切换一个。 我试图自己修复它,但我卡住了。我希望有人能指出我做错了什么。jQuery不隐藏其他DIV

这不是not(...)内部运行的命令:

$(document).ready(function() { 
    $('.extralink').click(function() { 
    $(this).closest('tr').next('tr.extra').toggle(); 

    $(this).not($(this).closest('tr').next('tr.extra')).hide; 
    }); 
} 

HTML

<table class="avs" border="1" width="384px"> 
     <tr class="avs"> 
      <th>Avatar name</th> 
      <th >Position</th> 
     </tr> 
     <tr class='avs'> 
      <td> 
       <a class='extralink' href="#"></a> 
      </td> 
      <td></td> 
     </tr> 
     <tr class='extra'> 
      <td colspan='2' > 
       <div> 
        <div>info</div> 
        <div>image</div> 
       </div> 
      </td> 
     </tr> 
<tr class='avs'> 
     <td> 
      <a class='extralink' href="#"></a> 
     </td> 
     <td></td> 
    </tr> 
    <tr class='extra'> 
     <td colspan='2' > 
      <div> 
       <div>info</div> 
       <div>image</div> 
      </div> 
     </td> 
    </tr> 
    </table>​ 
+0

你可以张贴HTML?另一个问题是,你隐藏的“$(this)”不是“下一个最接近的额外tr ......这意味着你要隐藏你点击的元素。 – bokonic

回答

0

如果你展示更多的HTML,它会有所帮助。但这里有一些指针来帮助你

$(document).ready(function() { 
    $('.extralink').click(function() { 
    // should cache the tr 
    $(this).closest('tr').next('tr.extra').toggle();  
    $(this).not($(this).closest('tr').next('tr.extra')).hide; //<--missing() 
    // usually not() isn't used with this - as anything with this is usually used inside not() to filter out 
    }); 
} 

因此,像这样会更好看

$(document).ready(function() { 
    $('.extralink').click(function() { 
    var $tr = $(this).closest('tr').next('tr.extra'); 
    $tr.toggle();  
    $('tr.extra').not($tr).hide(); // <-- I'm guessing this is correct 
    // since it's these tr's you are looking for to toggle 
    }); 
} 
+0

谢谢你,:) :) –

+0

然后接受他的答案,并upvote它。 – Yatrix

4

你错过了()的隐藏功能,这是需要说你实际上是调用它!

$(this).not($(this).closest('tr').next('tr.extra')).hide();