我試圖摺疊所有的孩子評論,包括父母評論時,當一些點擊圖標嵌套在家長評論內的評論。如何僅爲子元素運行jQuery函數?
使用下面的jQuery代碼,我能夠獲得評論框摺疊,但現在另一部分內的註釋也崩潰了。
jQuery代碼 -
$('.comment-toggle pre').on('click', function(e) {
$(".single-comment-wrapper .comment-text, .single-comment-wrapper .comment-bottom, .single-comment-outer .child-comment ").slideToggle('fast', function() {
if ($(this).is(':visible')) {
$(".comment-toggle pre").text('[–]');
} else {
$(".comment-toggle pre").text('[+]');
}
});
});
$('.comment-toggle pre').on('click', function(e) {
$('.single-comment-wrapper .left-side').slideToggle('fast');
});
由於HTMLand CSS太長。我創建了一個codepen。以下是它的直接鏈接。
https://codepen.io/anon/pen/Vzrvbm
在此先感謝。
感謝您的回答,但不幸的是,您的代碼無法正常工作。問題仍然存在 - a)現在,當評論崩潰時,它看起來像這樣 - http://i.imgur.com/BmL5tOX.png,但我想它看起來像這樣 - https://我。 imgur.com/P7mhuYc.png b)當我點擊第二個評論 - (https://i.imgur.com/E6D7B4E.png),它是下面所有評論的家長評論,它是摺疊上面的評論它也是如此。 – rowdy
已更新的答案。現在每個評論都會保存它的狀態,並且隱藏評論的內容也會隱藏。結束,您可以移除「parent-comment」類。 – Samich