<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
/* 设置div的宽度、高度和背景颜色 */
div {
width: 220px;
height: 220px;
background: rgb(81, 70, 184);
}
</style>
</head>
<body>
<div id="div1"></div>
<script>
//使用document对象调用getElementById()方法获取文档中的元素
var oDiv = document.getElementById("div1");
//光标移入div上时调用函数修改div样式
oDiv.onmouseover = changeStyle;
//光标移出div时调用函数恢复div最初样式
oDiv.onmouseout = resetStyle;
//定义函数,修改div的宽度和背景颜色
function changeStyle() {
oDiv.style.width = "400px";
oDiv.style.background = "#FCF";
}
//定义函数,将div的宽度和背景颜色恢复为最初状态
function resetStyle() {
oDiv.style.width = "220px";
oDiv.style.background = "rgb(81, 70, 184)";
}
</script>
</body>
</html>
本文来自投稿,不代表重蔚自留地立场,如若转载,请注明出处https://www.cwhello.com/53984.html
如有侵犯您的合法权益请发邮件951076433@qq.com联系删除