HTML和css
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css">
* {
margin: 0;
padding: 0;
}
ul {
width: 810px;
list-style: none;
margin-left: 20px;
}
li {
width: 200px;
line-height: 50px;
text-align: center;
border: 1px solid #ccc;
float: left;
}
.lis {
background: #999;
}
#sel {
width: 806px;
height: 100px;
border: 1px solid #ccc;
position: absolute;
top: 50px;
left: 20px;
}
#sel div {
width: 100%;
height: 100%;
display: none;
}
#sel div.cel {
width: 100%;
height: 100%;
display: block;
}
</style>
</head>
<body>
<ul id="ull">
<li class="lis">a</li>
<li>b</li>
<li>c</li>
<li>d</li>
</ul>
<div id="sel">
<div class="cel">
<p>hello,world</p>
</div>
<div>
<p>I LOVE YOU</p>
</div>
<div>
<p>how are you</p>
</div>
<div>
<p>I believe</p>
</div>
</div>
</body>
</html>
javascript代码
<script type="text/javascript">
window.function () {
//查找元素
var ul1 = document.getElementById('ull');
var box = document.getElementById('sel').children;
var index = 0;//保存被选中对象的索引值 并且默认为0
var str = ul1.children; //得到ul的子元素
for (var i = 0; i < str.length; i++) {
str[i]._index = i;//创建新的属性并赋值于i
str[i].onmouseenter = function() {
str[index].className = '';//清除上一个被选中添加的样式
box[index].className = '';
this.className = 'lis';//当前被选中添加的样式
box[this._index].className = 'cel';
index = this._index;//修改索引
}
}
}
</script>