jQuery Parent Sibling selector

前端之家收集整理的这篇文章主要介绍了jQuery Parent Sibling selector前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个如下的 HTML文档:
<td>
  <div class="commentLink">
    <a href="javascript:ShowBox()">Show</a>
  </div>
  <div class="hiddenBox">
    <!-- Hidden content  -->
  </div>
</td>

默认情况下隐藏class =“hiddenBox”的div.点击“显示链接,我想显示隐藏的div.我试过这个,但它不行.

function ShowBox() {
 $(function(){
  $(this).parent().siblings(".hiddenBox").show();
 });
}

类“hiddenBox”在我的文档中多次出现,但我只想同胞显示.

解决方法

问题在于,您的功能不会是< a>元件.你可以改变这样的内联处理程序来修复它:
<a href="#" onclick="ShowBox.call(this); return false;">Show</a>

啊只是注意到,另外你需要在准备处理程序之外引用这个.

function ShowBox() {
   var that = this;
   $(function(){
       $( that ).parent().siblings(".hiddenBox").show();
   });
}

我假设你已经设置了这种方式,以防有人在加载DOM之前单击链接.

猜你在找的jQuery相关文章