简体   繁体   中英

Display onload? What am I doing wrong?

    <div onload="showHidden();">
    aa
    </div>

    <div id="cantseeme" style="display: none;">LALALA</div>

<script>
      function showHidden() {
        document.getElementById("cantseeme").style.display='inline'; // Show it
      }
</script>

I'm trying it here: http://jsfiddle.net/Lisandro/7jMGH/

At the time showHidden() is called, the browser has not yet loaded the script that defines that function. You need to place the function definition before the call to it, or wait to call the function until after the script has loaded.

onload is not an attribute of the div element. It is only an attribute of the body element.

<div>
    aa
</div>

<div id="cantseeme" style="display: none;">LALALA</div>

<script>
        document.getElementById("cantseeme").style.display='inline'; // Show it
</script>

The script should load after the div so no need to worry about calling it.

I think.

http://jsfiddle.net/7jMGH/4/

There is no div "onload" You can add to your body onload and it should work.

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