如何在div之外定位多个类

时间:2018-06-19 01:39:03

标签: jquery

我需要通过单击标题(h2)来定位div之外的类。显然它不能使用.next()和.closest()。

   <div class="col-md-12">
     <div class="spacer m-t-40"></div>
     <h2 class="subtitle m-t-0 m-b-10">Who's Placing this ad?<h2>
   </div>
   <div class="accrodion-list container">
     <div class="row">
       <div class="col-md-12">
        <p class="sm-mb">(This will not be published)</p>
        <div class="spacer m-t-45"></div>
       </div>
     </div>
  </div> 
  <div class="col-md-12">
     <div class="spacer m-t-40"></div>
     <h2 class="subtitle m-t-0 m-b-10">Who is this ad for?<h2>
   </div>
   <div class="accrodion-list container">
     <div class="row">
       <div class="col-md-12">
        <p class="sm-mb">(This will not be published)</p>
        <div class="spacer m-t-45"></div>
       </div>
     </div>
  </div>  


$('.subtitle').each(function(){
        $(this).click( function() {
            var close = $(this).next('.accrodion-list');
                console.log($(close));
        })
    })

1 个答案:

答案 0 :(得分:0)

好的,我现在明白了。由于它具有单独的父div,因此我需要在执行next()

之前首先将其作为目标
$('.subtitle').each(function(){
    $(this).click( function() {
        var close = $(this).parents('div').next('.accrodion-list');
            console.log($(close));
            $(close).slideToggle();
    })
})