【发布时间】:2011-06-27 03:08:35
【问题描述】:
我找到了一个 jQuery 代码示例,我正在尝试调整和实现它。我正在创建一个常见问题解答页面,通过单击问题来切换答案。问题是 h1,答案是几个“p”元素。
像这样:
<h1>The First Question</h1>
<p>Answer Paragraph</p>
<p>Answer Paragraph</p>
<h1>Second Question</h1>
<p>Answer Paragraph</p>
<p>Answer Paragraph</p>
<p>Answer Paragraph</p>
我的JS代码是:
$(document).ready(function(){
$(".accordion h1:first").addClass("active");
$(".accordion p:not(:first)").hide();
$(".accordion h1").click(function(){
$(this).next("p").slideToggle("slow")
.siblings("p:visible").slideUp("slow");
$(this).toggleClass("active");
$(this).siblings("h1").removeClass("active");
});
});
问题是这个 JS 只切换页面上的第一个 p 元素。如何使用 NextAll() 切换属于某个问题的所有 p 元素?需要其他一切(兄弟姐妹等),我不能使用 div 或类。
谢谢!
【问题讨论】: