为什么我的函数不能按预期在文档加载上运行?

cmp*_*ger 5 html javascript height

此代码应设置元素高度; 但是没有添加样式.我错过了一些明显的东西吗

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

document.onload = setGround; 
Run Code Online (Sandbox Code Playgroud)

HTML非常基础:

<!DOCTYPE html>
<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 type="text/javascript" src="javascript/detect-css.js"></script>
</head>

<body>
    <header>
        <div id="logo"></div>
    </header>
    <section id="sidebar"><p>sf </p>
    </section>

    <section id="content"><p>sf </p>
    </section>

</body>
</html>
Run Code Online (Sandbox Code Playgroud)

谢谢您的帮助!

Mus*_*usa 4

不要使用document.onloadusewindow.onload来代替。

请参阅http://jsfiddle.net/mowglisanu/r6NzE/