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

时间:2015-03-24 18:04:07

标签: javascript jquery html css

您可以在此处查看我的网页示例: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>

1 个答案:

答案 0 :(得分:1)

由于我的链接不起作用,希望这就是你所追求的:

<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;
};

此致 大卫。