【发布时间】:2015-03-24 18:04:06
【问题描述】:
您可以在此处查看我的页面示例:https://dl.dropboxusercontent.com/u/24791073/TheAmericanWorker/index5.html。我正在尝试加载 MEC div,然后在单击顶部栏中的“最小值计划”等另一个链接时将其关闭。我试着让它“显示:块”而不是“显示:无”,但这不允许我关闭它。
这是我正在使用的 javascript 代码。
lastone = 'empty';
function showIt(lyr) {
if (lastone != 'empty') lastone.style.display = 'none';
lastone = document.getElementById(lyr);
lastone.style.display = 'block';
};
function hideIt(lyr) {
if (lastone != 'empty') lastone.style.display = 'block';
lastone = document.getElementById(lyr);
lastone.style.display = 'none';
}
以及链接代码示例:
<a href="JavaScript:"showonlyone";" onClick="showIt('MEC')" class="homeheader">MEC Plans
<div id="MEC" style="display:none;">
<p class=textwhheader>MEC Plans allow employers to offer qualifying coverage to their employees on a self-funded basis, which satisfies the individual mandate.
</p>
<br>
<a id='register'
class="btn red pure-button pure-button-primary"
style="font-size:13.33px; font-weight:700; line-height:26px; height:24px;vertical-align:middle;"
href='mecplans.html'
title="Click here to learn more.">
Learn More
</a>
</div>
【问题讨论】:
-
请整理好你的html代码,我相信你的代码干净整洁后你会自己发现问题的。
-
我清理了代码并查看了您的编辑。但是,这可以隐藏“display:block”,但是现在当我点击任何其他 div 时,它只隐藏“display:block”而没有“display:none”,这会导致其他 div保持开放。
-
我只编辑和整理了你的javascript代码。您的 html 包含严重错误。比如属性
href="JavaScript:"showonlyone";"就完全无效了。 -
制作
lastone = document.getElementById('MEC');而不是empty,因为您默认显示 MEC 之一。或者您可以在页面加载时致电showIt('MEC')。 -
这是我可以做到的一种方式。 jsfiddle.net/nqu46a34/9
标签: javascript jquery html css