1. 首页>>前端>>javascript

window.onload 代码案例

  在这个例子中,有一个高度为 300 像素、宽度为 300 像素的 div 元素。在这里,我们使用window.onload()在加载网页后更改div元素的背景颜色、宽度和高度。

  背景颜色设置为'blue',宽度和高度分别设置为500px。

<style type = "text/css">  
#bg{  
    width: 300px;  
    height: 300px;  
}  
</style>  
<script type = "text/javascript">  
    window.onload = function(){  
        document.getElementById("bg").style.backgroundColor = "blue";  
        document.getElementById("bg").style.width = "500px";  
        document.getElementById("bg").style.height = "500px";  
}  
</script>  
</head>  
<body>  
<h2> 这是window.onload() 代码案例</h2>  
<div id = "bg"></div>  
</body>

效果:

window.onload 代码案例

转载联系作者并注明出处:https://www.focusonseo.cn/jcczs/109.html