Why isn't my function running on document load as expected?

喜夏-厌秋 提交于 2019-12-04 07:43:31

Don't use document.onload use window.onload instead.

See http://jsfiddle.net/mowglisanu/r6NzE/

you can use this:

function setGround() { 
    document.getElementById('content').style.height = '40px';
} 
document.onload = setGround;

but if you want see changes, you should create border on section tag by use this:

<section id="content" style='border:1px solid fuchsia;' >
<p>sf </p>
</section>     

You should use

window.onLoad = setGround; 

instead of

document.onload = setGround;

Where have you placed the javascript-code? Is it in the detect-css.js?

This works:

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Template</title>
<link rel="stylesheet" type="text/css" href="css/default.css"/>
<link rel="stylesheet" type="text/css" href="css/orange.css"/>
<script language="javascript">
function resize(){
document.getElementById("content").style.height='100px';
}
</script>
</head>

<body onload="resize()">
<header><div id="logo"></div></header>
<section id="sidebar"><p>sf </p>
</section>

<section id="content" style="background-color:#CCC; display:block;"><p>sf </p>
</section>

</body>
</html>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!