gpt4 book ai didi

javascript - 如何使 javascript div 在加载时打开但在单击另一个 div 链接时隐藏?

转载 作者:行者123 更新时间:2023-11-28 17:10:56 25 4
gpt4 key购买 nike

你可以在这里看到我的页面示例:https://dl.dropboxusercontent.com/u/24791073/TheAmericanWorker/index5.html .我正在尝试加载 MEC div,然后在单击顶部栏中的另一个链接(例如最低值(value)计划)时将其关闭。我尝试只使用“display:block”而不是“display:none”,但这不允许我关闭它。

这是我正在使用的 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>

最佳答案

由于我的链接失效了,希望这是你想要的:

<ul>
<li>
<a id="first option" onClick="toggleDiv('myDiv1');">Option 1</a>
</li>
<li>
<a id="first option" onClick="toggleDiv('myDiv2');">Option 2</a>
</li>
</ul>
<div id="myDiv1">
<span>Some data here</span>
</div>
<div id="myDiv2" style="display:none">
<span>Div 2 data</span>
</div>

var currentActiveDiv = 'myDiv1';

toggleDiv = function(id){
var domElement = document.getElementById(id);

if (currentActiveDiv && currentActiveDiv !== id){
var elementToHide = document.getElementById(currentActiveDiv);
elementToHide.style.display = 'none';
}

domElement.style.display = 'block';
currentActiveDiv = id;
};

问候,大卫。

关于javascript - 如何使 javascript div 在加载时打开但在单击另一个 div 链接时隐藏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29239803/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com