<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.bg{
color: red;
}
</style>
</head>
<body>
<p>我是一段文字</p>
<p>我是一段文字</p>
<p>我是一段文字</p>
<p>我是一段文字</p>
<script>
var ps = document.getElementsByTagName("p")
for(var i in ps){
ps[i].onmouseover = function(){
this.setAttribute("class","bg")
}
//为每一个p标签添加鼠标离开事件
ps[i].onmouseout = function(){
this.removeAttribute("class")
}
}
</script>
</body>
</html>