我正在創建一個Q &測試準備,有幾個問題,每個問題都有四個可能的答案,然後在答案下方有一個「答案」按鈕,顯示真實答案。我得到了一切正常工作,直到我意識到我有它,所以任何「答案」按鈕將切換所有的內容DIVs。我怎樣才能讓「答案」按鈕只打開前面的「內容」div?切換DIV而不切換所有名稱相似的DIV
請記住,我不能指定唯一的ID,因爲有數百個問題,我想,以節省時間。
的jQuery:
$(function() {
$(".answer").click(function() {
$(".content").toggle("slow");
});
});
CSS:
div.content {
display: none;
width: 300px;
height: auto;
margin: 10px;
padding: 20px;
background: white;
border: 1px solid black;
cursor: pointer;
}
button.answer {
font-family: sans-serif;
font-weight: bold;
font-style: normal;
font-size: 0.92em;
line-height: 1.36em;
text-indent: 0em;
text-align: left;
color: #000000;
margin: 1em 0em 0em 2em;
}
HTML:
<div class="keep">
<p class="q"><samp class="q-no">1.</samp> Interpret the following directions:</p>
<p class="q-equation">i cap po qid × 10d</p>
<p class="an"><span class="choice">a.</span> Take one capsule by mouth four times a day for ten days.</p>
<p class="an"><span class="choice">b.</span> Take one capsule by mouth three times a day for ten days.</p>
<p class="an"><span class="choice">c.</span> Take one capsule by mouth twice a day for ten days.</p>
<p class="an"><span class="choice">d.</span> Take one capsule by mouth once a day for ten days.</p>
<div class="content">
<p class="anl"><b>a.</b> Take one capsule by mouth four times a day for ten days. Remember: <i>qd</i> is once a day, <i>bid</i> is twice a day, <i>tid</i> is three times a day and <i>qid</i> is four times a day.</p><a id="anchor-25-anchor"></a>
</div>
<button class="answer">Answer</button>
</div>
<div class="keep">
<p class="q"><samp class="q-no">2.</samp> Interpret the following directions:</p>
<p class="q-equation">ii tab po tid × 7d.</p>
<p class="an"><span class="choice">a.</span> Take two tablets by mouth four times a day for seven days.</p>
<p class="an"><span class="choice">b.</span> Take two tablets by mouth three times a day for seven days.</p>
<p class="an"><span class="choice">c.</span> Take two tablets by mouth twice a day for seven days.</p>
<p class="an"><span class="choice">d.</span> Take two tablets by mouth once a day for seven days.</p>
<div class="content">
<p class="anl" id="anchor-25-anchor"><b>b.</b> Take two tablets by mouth three times a day for seven days.</p><a id="anchor-26-anchor"></a>
</div>
<button class="answer">Answer</button>
</div>
甜!謝謝,這很好,而且非常簡單。我對jQuery非常新穎,這非常有幫助。我一直在谷歌搜索了六個小時,發現沒有任何工作。 – Fluketyfluke