<!DOCTYPE html>
<html>
<head>
<title></title>
<style type="text/css">
/*<!-- *选中所有标签 -->*/
*{
padding: 0;
margin: 0;
}
ul{
list-style: none;
overflow: hidden;
}
ul li{
float: left;
100px;
height: 100px;
margin-left: 10px;
margin-top: 20px;
border: 2px solid #fff;
}
ul li.active{
border-color: red;
}
</style>
</head>
<body>
<img src="./image/1.jpg" id="bigImg" width="400px" height="400px">
<ul>
<li class="active">
<a href=""><img src="./image/2.jpg" width="100px" class="smallImg"></a>
</li>
<li>
<a href=""><img src="./image/3.jpg"width="100px"class="smallImg"></a>
</li>
<li>
<a href=""><img src="./image/4.jpg"width="100px"class="smallImg"></a>
</li>
<li>
<a href=""><img src="./image/5.jpg"width="100px"class="smallImg"></a>
</li>
</ul>
<script type="text/javascript">
//1.获取事件源
var bigImg = document.getElementById('bigImg');
var smallImg = document.getElementsByClassName('smallImg');
for(var i = 0;i < smallImg.length;i++){
//遍历集合,给每个img标签添加事件
smallImg[i].onmouseover = function(){
//再悬浮到每个li标签之前,先把所有的li标签的类名都置为空值
for(var j = 0;j < smallImg.length;j++){
smallImg[j].parentNode.parentNode.setAttribute('class','');
}
//修改大图的src属性值
var smallImgsrc = this.getAttribute('src');
bigImg.setAttribute('src',smallImgsrc);
//给鼠标悬浮的img标签的父标签添加类
this.parentNode.parentNode.setAttribute('class','active');
}
}
</script>
</body>
</html>