[英]how do i show a div on clicking another div and hide it on clicking that same div?
[英]How do I make a javascript div open on load but hide when clicking on another div link?
您可以在這里看到我的頁面示例: https : //dl.dropboxusercontent.com/u/24791073/TheAmericanWorker/index5.html 。 我正在嘗試加載MEC div,然后在單擊頂部欄中的其他鏈接(如“最低價值計划”)時將其關閉。 我嘗試僅使用它“ 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;
};
問候,大衛。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.