<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
*{
margin: 0;
padding: 0;
}
.yellow{
width: 150px;
height: 20px;
background-color: greenyellow;
}
div{
font-size: 16px;
width: 150px;
height: 20px;
text-align: center;
}
</style>
</head>
<body>
<p>你最喜欢哪首歌?</p>
<div id="div1">If you</div>
<div>晴天</div>
<div>let's go</div>
<div>你最红</div>
<div>lonely</div>
<script type="text/javascript">
var aDiv = document.getElementsByTagName("div");
// this 关键字: 事件绑定时,在事件内, 指代被绑定的对象。
for (i = 0; i < aDiv.length; i++) {
aDiv[i].onmouseover = function(){
this.className = "yellow";
}
aDiv[i].onmouseout = function (){
this.className = "";
}
}
</script>
</body>
</html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
*{
margin: 0;
padding: 0;
}
.yellow{
width: 150px;
height: 20px;
background-color: greenyellow;
}
div{
font-size: 16px;
width: 150px;
height: 20px;
text-align: center;
}
</style>
</head>
<body>
<p>你最喜欢哪首歌?</p>
<div id="div1">If you</div>
<div>晴天</div>
<div>let's go</div>
<div>你最红</div>
<div>lonely</div>
<script type="text/javascript">
var aDiv = document.getElementsByTagName("div");
// this 关键字: 事件绑定时,在事件内, 指代被绑定的对象。
for (i = 0; i < aDiv.length; i++) {
aDiv[i].onmouseover = function(){
this.className = "yellow";
}
aDiv[i].onmouseout = function (){
this.className = "";
}
}
</script>
</body>
</html>