2017-10-14 185 views
0

我想要點擊button我的div與類text-internal(具體一個 - 我我有多於一個div類text-internal)獲得max-height。正如我所說的只有特定的div應該獲得max-height - 只有這個,這是在容器中,被點擊,其他人,這是沒有點擊沒有獲得max-height 我不知道我是否正確解釋清楚。jQuery,添加css規則到特定元素點擊

<div class="single-offer-text"> 
     <div class="text-internal"> 
      <?php echo $params->get('mytextarea'); ?> 
     </div> 
     <button class="read-more">Read more</button> 
    </div> 

JQUERY

jQuery('.read-more-hide').click(function() { 
    jQuery('.text-internal').css('maxHeight', '150px'); 
}); 

回答

1

使用任意數量的遍歷方法來分離特定的實例。

prev()是一個簡單的一個適合您的方案

jQuery('.read-more-hide').click(function() { 
    jQuery(this).prev('.text-internal').css('maxHeight', '150px'); 
}); 

或者,如果關係不是那麼簡單的,你可以使用closest()然後find()該父

jQuery('.read-more-hide').click(function() { 
    jQuery(this).closest('.single-offer-text').find('.text-internal').css('maxHeight', '150px'); 
}); 
+0

'內穿越到一個共同的父jQuery.prev'?這是新的還是你的意思是'jQuery(this).prev'? – llama

+0

@llama是錯字...謝謝 – charlietfl