【问题标题】:Hiding based on onclick with only one open基于onclick的隐藏,只打开一个
【发布时间】:2012-08-04 18:51:18
【问题描述】:

我有这个小 javascript 代码,我正在尝试操作:

<script type="text/javascript" language="JavaScript"><!--
function HideContent(d) {
document.getElementById(d).style.display = "none";
}
function ShowContent(d) {
document.getElementById(d).style.display = "block";
}
function ReverseDisplay(d) {
if(document.getElementById(d).style.display == "none") { document.getElementById(d).style.display = "block"; }
else { document.getElementById(d).style.display = "none"; }
}
//--></script>

我试图用它做一个带有多个答案的小问题,每次点击显示一条消息...

所以我构建了这段代码:

<table class="hovertable">
<tr class="hovertable">
<td colspan="4" class="deep">What is the Generic Name of this Medication?</td>
</tr>
<tr onclick="javascript:ReverseDisplay('question1a');">
<td class="age">A.</td><td colspan="3" class="notes" onmouseover="this.style.backgroundColor='#ffba74';" onmouseout="this.style.backgroundColor='#FFFFFF';">
Lovastatin.</td>
</tr>
<tr onclick="javascript:ReverseDisplay('question1b');">
<td class="age">B.</td><td colspan="3" class="notes" onmouseover="this.style.backgroundColor='#ffba74';" onmouseout="this.style.backgroundColor='#FFFFFF';">
Atorvastatin.</td>
</tr>
<tr onclick="javascript:ReverseDisplay('question1c');">
<td class="age">C.</td><td colspan="3" class="notes" onmouseover="this.style.backgroundColor='#ffba74';" onmouseout="this.style.backgroundColor='#FFFFFF';">
Simvastatin.</td>
 </tr>
 <tr onclick="javascript:ReverseDisplay('question1d');">
<td class="age">D.</td><td colspan="3" class="notes" onmouseover="this.style.backgroundColor='#ffba74';" onmouseout="this.style.backgroundColor='#FFFFFF';">
Pravastatin</td>
 </tr>
 </table>

这里是显示消息的部分。

<div id="question1a" style="display:none;">
<table class="hovertable">
<tr>
<td class="age">Answer</td><td colspan="3" class="notes" onmouseover="this.style.backgroundColor='#ffba74';" onmouseout="this.style.backgroundColor='#FFFFFF';">
This is Correct.</td>
</tr>
</table></div>
<div id="question1b" style="display:none;">
<table class="hovertable">
<tr>
<td class="age">Answer</td><td colspan="3" class="notes" onmouseover="this.style.backgroundColor='#ffba74';" onmouseout="this.style.backgroundColor='#FFFFFF';">
This is Not Correct</td>
</tr>
</table></div>
<div id="question1c" style="display:none;">
<table class="hovertable">
<tr>
<td class="age">Answer</td><td colspan="3" class="notes" onmouseover="this.style.backgroundColor='#ffba74';" onmouseout="this.style.backgroundColor='#FFFFFF';">
This is Not Correct</td>
</tr>
</table></div>
<div id="question1d" style="display:none;">
<table class="hovertable">
<tr>
<td class="age">Answer</td><td colspan="3" class="notes" onmouseover="this.style.backgroundColor='#ffba74';" onmouseout="this.style.backgroundColor='#FFFFFF';">
This is Not Correct</td>
</tr>
</table></div>

我想弄清楚的是如何一次只显示一条消息。因此,在您单击其中一个答案之前,什么都没有,但如果您单击一个,则会显示消息。但是,如果您单击另一个它会消失,并且您单击的那个消息会出现。我希望这不会令人困惑。

谁能帮帮我?

【问题讨论】:

    标签: javascript html-table onclick


    【解决方案1】:

    你最好用 jQuery 之类的东西来做 DOM 操作。

    它会像这样工作

    $("#question1").show().siblings().hide();
    

    http://api.jquery.com/siblings/

    【讨论】:

    • 我会调查的。谢谢。
    • 我想通了就接受。哈哈。我对 jquery 一无所知。
    • 这是左边上下投票按钮下的复选框...谢谢:)
    • 啊,jQuery 真的是你应该学习的 Javascript 框架。它使与 DOM(在浏览器中呈现的 HTML 元素)的交互变得非常容易,并且包括 CSS 选择器、AJAX、事件和 DOM 操作等内容。它也适用于所有浏览器。看看吧!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-05-15
    • 2020-02-23
    • 1970-01-01
    • 2020-07-19
    • 2014-11-19
    • 2017-08-06
    • 2012-12-23
    相关资源
    最近更新 更多