jQuery 控制具有相同类的单个 DIV(兄弟姐妹?)
我正在为我的网站构建一个常见问题解答模块,我希望能够控制页面上的单个元素,即使它们都具有相同的类。 我相信这属于我还不熟悉的兄弟姐妹。
基本上,我希望用户能够单击问题 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>
I am building a FAQ module for my site and I want to be able to control single elements on the page even though they all have the same class. I believe this comes under siblings which I am not yet familiar with.
Basically I want the user to be able to click the question div and then when they click it the answer div within the same div as the question div is set to show (if that makes sense!). Any help would be greatly appreciated.
<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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果我正确理解你的问题,你应该
首先将所有答案设置为隐藏在 CSS 中:
.answer {display:none;}
然后您可以使用 jquery 显示单击问题的正确答案:
编辑:您还可以使用 .toggle() 而不是 .show() 来显示/隐藏。
If I understand your question correctly, you should
Start by setting all answers as hidden in the css:
.answer {display:none;}
Then you can use jquery to show the correct answer to the clicked questions :
Edit: you can also use .toggle() instead of .show() to show/hide.
您可能应该查看这个问题,其中完成了类似的操作。
基本上,您首先需要为元素设置 ID,以便可以识别集合类中的单个元素。
然后,您可以添加一个单击事件处理程序,该处理程序将设置所选项目并显示适当的答案。
您可以在此处的文档中查看抓取同级的语法。
You should probably check out this question where something similar is done.
Basically, you first need to setup ID's for your elements so that you can identify single elements within the set classes.
You could then add a click event handler which would set the selected item and show the appropriate answer.
You can see the syntax for grabbing siblings in the documentation here.