简体   繁体   中英

Collapsing JavaScript/HTML div on page load?

I need help collapsing a collapsible div on page load.

I'm using this JavaScript code:

<script type="text/javascript">
    function switchMenu(obj) {
        var el = document.getElementById(obj);
        if ( el.style.display != "none" ) {
            el.style.display = 'none';
        }
        else {
            el.style.display = '';
        }
    }
    document.getElementById('aboutme').style.display = 'none';
</script>

to collapse HTML div id="aboutme" when the <a ...>about me</a> is clicked:

<div class="container">
    <a href="#" onclick="switchMenu('aboutme');">about me</a>
    <div id="aboutme">
        sample text to be expanded and collapsed
    </div>
</div>

I can't get the page to close my div#aboutme on page load.

I want this page to load with my div collapsed.

I thought that the JS line

document.getElementById('aboutme').style.display = 'none';

should do the trick but it doesn't. What am I doing wrong?

Thanks for your help.

If you want your div to load collapsed, simply write the following

 <div id="aboutme" style="display:none">
        sample text to be expanded and collapsed
    </div>

This should resolve the problem. However, if you are still interested in the JavaScript solution keep reading.
As you said I can't get the page to close my div#aboutme on page load - the problem is that you are not using "onload" event. Simply put the line document.getElementById('aboutme').style.display = 'none'; in your body's onload attribute.. something like this

<body onload="document.getElementById('aboutme').style.display = 'none';">
...
</body>

and you should see the results with JavaScript. I recommend you use "style" method instead. much better.

Exactly how do you make that JS run on window load? It may simply run before the page is rendered

Does clicking on the link work? if it does, that would prove that the issue is simply the loading sequence

The easiest solution would be to place your code at the very end of your HTML file, just before the closing </body> tag. The code below is more generic, and can be placed anywhere. Note that to toggle the link back on I set the display to 'inline' (or block, ie whatever it was before - you may want to save that to a variable to be sure)

<script type="text/javascript">
function switchMenu(id) {
    var el = document.getElementById(id);
    if ( el.style.display != "none" ) {
        el.style.display = 'none';
    }
    else {
        el.style.display = 'inline'; //or block - i.e. whatever it is rendered by
    }
}

//add to the window onload event
if( window.addEventListener ){
    window.addEventListener( 'load', function(){ switchMenu('aboutme')}, false);
} else if (window.attachEvent) {
    window.attachEvent("onload", function(){ switchMenu('aboutme') } );
}
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM