【发布时间】:2011-12-14 15:10:53
【问题描述】:
我在这个项目的早期问题上得到了一些很大的帮助,但现在它变得更加复杂(至少对我而言)。这是一个问答项目,其中包含一个问题、四个可能的答案以及下方的答案按钮,该按钮将在隐藏在按钮上方的 div 中显示答案。当答案实际上在 div 中时,我做得很好,但现在我需要尝试将答案从外部 HTML 文件加载到现在为空的 div 中。 jQuery 的 load() 函数在浏览器中运行良好,然而,这个项目是 iBooks 的增强型 epub 文件,显然不允许 load() 函数(但它确实允许 jQuery)。
是否有另一种方法可以在不使用 load() 的情况下将元素 ID 加载到 DIV 中?另外,既然这变得更加复杂,我如何在打开另一个 div 时切换关闭前一个 div?
jQuery:
$(function() {
$(".answer").click(function() {
$(this).prev(".content").load($(this).attr('href')).toggle("slow");
return false; // And prevent it following the link
});
});
CSS:
div.content {
display: none; /*--hidden by default--*/
width: 300px;
height: auto;
margin: 10px;
padding: 20px;
background: white;
border: 1px solid black;
cursor: pointer;
}
a.answer {
font-family: sans-serif;
font-weight: bold;
font-style: normal;
font-size: 0.92em;
line-height: 3em;
text-indent: 0em;
text-align: left;
margin: 1em 0em 0em 1em;
}
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">
</div>
<a class="answer" href="04b-Ch4-Answers.html #anchor-24-anchor">Answer</a>
</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">
</div>
<a class="answer" href="04b-Ch4-Answers.html #anchor-25-anchor">Answer</a>
</div>
提前感谢您的帮助。
【问题讨论】:
-
任何 AJAX 请求都有效吗?如果不是,您将需要使用服务器端语言。
-
老实说,我不确定任何 AJAX 请求是否可以在 epub 中工作。这是我唯一尝试过的。不幸的是,因为这是一个 epub 文件,服务器端语言将不适用。对于符合 EPUB3 标准的电子书来说,Javascript 本身就是一个全新的东西。
标签: jquery