2014-09-25 20 views
-1

sample如何使JavaScript類撥動持續

var main = function() { 
    $('.article').click(function() { 
     if($(this).hasClass('current')) { 
       $(this).children('.description').toggle("slow"); 
     } 
     else { 
       $('.article').removeClass('current'); 
       $('.description').hide(); 
       $(this).addClass('current'); 
       $(this).children('.description').toggle("slow"); 
     }; 
}); 
} 
$(document).ready(main); 

你好,我想知道如何學會做幾件事情跟我一點就點擊:具有

林在用戶點擊一個已經打開的文章之後,如何使這個切換列表保持持久性是困難的。現在我擁有它的方式設置當前的文章將會在每次點擊時切換,我希望它保持打開狀態,並且只需點擊標題本身(與項目中的任何位置相對)即可關閉。

而且
+如何建立一個開放/關閉所有的肘杆
+連續性的視覺線索,顯示列表是可擴展的,如果它是打開或關閉,如果不這樣做,將適合這種風格

+0

切換切換 - 就是這樣。對於持久性,您需要addClass/removeClass,而不是切換。 – 2014-09-25 17:46:15

+0

然後將切換處理程序附加到'.article'類可能不是您想要的,如果您想要以不同方式處理關閉切換。 – 2014-09-25 18:08:18

+0

感謝您的輸入!林仍然不知道爲什麼我得到了一個合法的問題,我的負面問題.. – 2014-09-25 19:01:32

回答

1

您需要針對.article .item,而不是.article

var main = function() { 
    $('.article .item').click(function() { 
     var article = $(this).closest('.article'); 
     if(article.hasClass('current')) { 
       article.children('.description').toggle("slow"); 
     } 
     else { 
       $('.article').removeClass('current'); 
       $('.description').hide(); 
       article.addClass('current'); 
       article.children('.description').toggle("slow"); 
     }; 
    }); 
} 

演示在http://jsfiddle.net/kL70z8ky/5/

+0

謝謝!非常簡單的解決方案,現在我明白我的錯誤 – 2014-09-25 19:08:41

1

想要點擊一個子項來關閉主標題,則需要停止事件傳播。

請查看完整的語法/用法:How to stop event bubbling on checkbox click

用法:

event.stopPropagation() 
+0

明白了,很好的資源謝謝! – 2014-09-25 19:04:38

1

這裏有一個不錯的aproa ch:

$('.article').click(function() { 
    $(".article").not($(this).addClass('current')).removeClass('current'); 
    $('.description').not($('.description',this).slideDown()).slideUp(); 
});