2013-06-22 49 views
0

我一直运行到这个问题,因为热闹的短期记忆丧失......选择DIV

我试图做的是slideToggle驻留在相同的父元素作为按钮div元素。

每个加载的页面上都有多个html结构,因此选择正确的相应结构是目标。

<div class="exBtn"> 
    <a href="#"> 
    <img src="<?php echo get_template_directory_uri(); ?>/media/showTextButton.png"> 
    </a> 
</div> 

<div class="initialPostLoad"> 
    <?php the_content('Read the rest of this entry &raquo;'); ?> 
</div> <!-- end initialPostLoad --> 


    $(document).ready(function() { 
     $('.exBtn a').click(function (event) { 
      event.preventDefault(); 
      $(this).parent().closest('.initialPostLoad').slideToggle(); 
     }); 
    }); 

回答

3
$(this).parent().closest('.initialPostLoad').slideToggle(); 

你不想closestclosest的意思是“在这个元素的父母之间搜索”。你想在兄弟姐妹中搜索。事实上,你想要做的是获得下一个兄弟姐妹。这很简单:只需使用next

$(this).parent().next().slideToggle(); 
+0

啊啊我一直对父母,孩子和兄弟姐妹感到困惑。这来自不是独生子的人... – canacast

+1

@canacast也许你来自诺福克... – lonesomeday

0

使用.next()

$(this).parent().next('.initialPostLoad').slideToggle(); 

.initialPostLoad旁边是你的exBtn