jQuery控制具有相同类的单个div(同级?)

jQuery控制具有相同类的单个div(同级?),jquery,siblings,Jquery,Siblings,我正在为我的站点构建一个FAQ模块,我希望能够控制页面上的单个元素,即使它们都具有相同的类。我相信这是在我还不熟悉的兄弟姐妹之下 基本上,我希望用户能够单击问题div,然后当他们单击它时,问题div设置为显示的同一div中的答案div(如果有意义的话!)。任何帮助都将不胜感激 <div class="set"> <div class="question">What is the airspeed velocity of an unladen swallow?</

我正在为我的站点构建一个FAQ模块,我希望能够控制页面上的单个元素,即使它们都具有相同的类。我相信这是在我还不熟悉的兄弟姐妹之下

基本上,我希望用户能够单击问题div,然后当他们单击它时,问题div设置为显示的同一div中的答案div(如果有意义的话!)。任何帮助都将不胜感激

<div class="set">
  <div class="question">What is the airspeed velocity of an unladen swallow?</div>
  <div class="answer">Although a definitive answer would of course require further measurements, published species-wide averages of wing length and body mass, initial Strouhal estimates based on those averages and cross-species comparisons, the Lund wind tunnel study of birds flying at a range of speeds, and revised Strouhal numbers based on that study all lead me to estimate that the average cruising airspeed velocity of an unladen European Swallow is roughly 11 meters per second, or 24 miles an hour. </div>
</div>

<div class="set">
  <div class="question">What is the airspeed velocity of an unladen swallow?</div>
  <div class="answer">Although a definitive answer would of course require further measurements, published species-wide averages of wing length and body mass, initial Strouhal estimates based on those averages and cross-species comparisons, the Lund wind tunnel study of birds flying at a range of speeds, and revised Strouhal numbers based on that study all lead me to estimate that the average cruising airspeed velocity of an unladen European Swallow is roughly 11 meters per second, or 24 miles an hour. </div>
</div>

<div class="set">
  <div class="question">What is the airspeed velocity of an unladen swallow?</div>
  <div class="answer">Although a definitive answer would of course require further measurements, published species-wide averages of wing length and body mass, initial Strouhal estimates based on those averages and cross-species comparisons, the Lund wind tunnel study of birds flying at a range of speeds, and revised Strouhal numbers based on that study all lead me to estimate that the average cruising airspeed velocity of an unladen European Swallow is roughly 11 meters per second, or 24 miles an hour. </div>
</div>

空载燕子的空速是多少?
虽然最终答案当然需要进一步测量、公布的全物种平均翼长和体重、基于这些平均值和跨物种比较的初步斯特劳哈尔估计、隆德风洞对不同速度飞行鸟类的研究,基于这项研究修正的斯特劳哈尔数据都让我估计,一只空载的欧洲燕子的平均巡航空速约为每秒11米,或每小时24英里。
空载燕子的空速是多少?
虽然最终答案当然需要进一步测量、公布的全物种平均翼长和体重、基于这些平均值和跨物种比较的初步斯特劳哈尔估计、隆德风洞对不同速度飞行鸟类的研究,基于这项研究修正的斯特劳哈尔数据都让我估计,一只空载的欧洲燕子的平均巡航空速约为每秒11米,或每小时24英里。
空载燕子的空速是多少?
虽然最终答案当然需要进一步测量、公布的全物种平均翼长和体重、基于这些平均值和跨物种比较的初步斯特劳哈尔估计、隆德风洞对不同速度飞行鸟类的研究,基于这项研究修正的斯特劳哈尔数据都让我估计,一只空载的欧洲燕子的平均巡航空速约为每秒11米,或每小时24英里。

如果我正确理解您的问题,您应该 首先将所有答案设置为隐藏在css中: .答案{显示:无;}

然后,您可以使用jquery显示所单击问题的正确答案:

$(document).ready ( function () {
    $('.question').click(function() {
        $(this).next('.answer').show();
    });
});

编辑:您也可以使用.toggle()而不是.show()来显示/隐藏。

您可能应该在这里查看类似的操作

基本上,您首先需要为元素设置ID,以便可以识别集合类中的单个元素

然后可以添加一个click事件处理程序,该处理程序将设置所选项目并显示相应的答案

您可以在中看到抓取兄弟姐妹的语法