2015-10-30 31 views
1

我有兩個部分,即JQuery的切換用加號和減號圖標

1. section one 

2. section two 

我進行JQuery的向上滑動,並加上兩個DIV部分之間滑下去,負change..currently簽署工作寄望我想在我的代碼中進行一個小的更改時,我點擊第二節中的一個內容是隱藏的,反之亦然,我需要兩者都應該同時打開,有人可以幫助我實現它。謝謝!

http://jsfiddle.net/v9Evw/348/

HTML

<ul id="toggle-view"> 
    <li > 
     <h3 style='background:#4c97d0;color:#fff;'>Section one</h3> 
     <span style='background:#4c97d0;color:#fff;' class='glyphicon glyphicon-plus'></span> 
     <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Morbi commodo, ipsum sed pharetra gravida, orci magna rhoncus neque, id pulvinar odio lorem non turpis. Nullam sit amet enim.</p> 
    </li> 
    <br> 
    <li> 
     <h3 style='background:#4c97d0;color:#fff;'>section two</h3> 
     <span style='background:#4c97d0;color:#fff;' class='glyphicon glyphicon-plus'></span> 
     <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Morbi commodo, ipsum sed pharetra gravida, orci magna rhoncus neque, id pulvinar odio lorem non turpis. Nullam sit amet enim.</p> 
    </li> 
</ul> 

JQUERY

var parent = $('#toggle-view'), // storing main ul for use below 
    delay = 200; // storing delay for easier configuration 

// bind the click to all headers 
$('li h3, li span', parent).click(function() { 

    // get the li that this header belongs to 
    var li = $(this).closest('li'); 

    // check to see if this li is not already being displayed 
    if (!$('p', li).is(':visible')) 
    { 
     // loop on all the li elements 
     $('li', parent).each(function() { 

      // slide up the element and set it's marker to '+' 
      $('p', $(this)).slideUp(delay); 

     }); 

     // display the current li with a '-' marker 
     $('p', li).slideDown(delay); 

    } 
    else { 
     $('p', li).slideUp(delay); 

    } 
    $('span',li).toggleClass('glyphicon-plus glyphicon-minus'); 
}); 

回答

2

刪除其變化上的所有內容領域國家循環。您只想將其應用於所選內容。

所以現在你說的是;如果這是開放的,關閉它,否則打​​開它。

if (!$('p', li).is(':visible')) { 

    $('p', li).slideDown(delay); 

} else { 

    $('p', li).slideUp(delay); 

} 

http://jsfiddle.net/v9Evw/350/

+0

非常感謝你Partypete25 ... – Sjay

1

請找到更新的小提琴
http://jsfiddle.net/v9Evw/349/

//$('li', parent).each(function() { 

     // slide up the element and set it's marker to '+' 
     $('p', $(this)).slideUp(delay); 

    // }); 

只有選定的部分將下滑,而不是部分1和2,請查看是否符合要求,烏爾

+0

太感謝你了TR哈.. – Sjay