实例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>我的家乡--福州</title> <style type="text/css"> .box { width: 500px; height: 700px; background-color: #efefef; border: 1px solid lightgray; margin: 20px auto; text-align: center; color: #636363; box-shadow: 2px 2px 2px #999; } .box ul { margin:0; padding:0; /*将ul转为BFC独立块,使之不受内部浮动元素的影响*/ overflow: hidden; } .box ul li { list-style-type: none; float:left; background-color: skyblue; margin-left: 20px; } .box ul li a { /*将a转为块元素,并设置为li的宽高,这样可以使整个li可以被点击*/ display: block; width: 100px; height: 40px; line-height: 40px; color: white; text-decoration: none; } .box ul li:hover { font-size:1.2em; background-color: coral; } .active { font-size:1.2em; background-color: coral; } .box .pic { width: 450px; height:450px; border: 1px solid lightgray; margin: 50px auto 0; } .box .pic img { width: 100%; height: 100%; } </style> </head> <body> <div class="box"> <h2>我的家乡</h2> <ul> <li> <a href="images/3f7x.png" title="三坊七巷,镇海楼,三山两塔..." >风景名胜</a> </li> <li> <a href="images/lzx.png" title="林则徐,冰心,林觉民....">历史名人</a> </li> <li> <a href="images/ftq.png" title="佛跳墙,肉燕,鼎边糊....">风味美食</a> </li> </ul> <div class="pic"> <img src="images/zwt.png" alt="" id="img"> </div> <p id='info'></p> </div> <script> var lia = document.getElementsByTagName('a'); var img = document.getElementById('img'); var p = document.getElementById('info'); for (var i=0; i < lia.length;i++) { lia[i].onclick = function () { //移除所有用户锁定功能,并为当前点击增加锁定 for (var i = 0; i < lia.length; i++) { lia[i].classList.remove('active'); } this.classList.add('active'); var picUrl = this.href; var picInfo = this.title; var picName = this.innerHTML; img.src = picUrl; p.innerHTML = '<span style="color:coral">' + picName + ':' + picInfo + '</span>'; return false; }; } </script> </body> </html>
运行实例 »
点击 "运行实例" 按钮查看在线实例