实例
<!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; overflow: hidden; } .box ul li { list-style-type: none; float:left; background-color: skyblue; margin-left: 20px; } .box ul li a { 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; } .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/zly.jpg" title="《楚乔传》,《花千骨》,《陆贞传奇》..">赵丽颖</a> </li> <li> <a href="images/gyy.jpg" title="《倚天屠龙记》,《咱们经婚吧》,《爱无悔》...">高圆圆</a> </li> <li> <a href="images/sl.jpg" title="《那年花开月正圆》,《甑环传》,《玉观音》..." >孙俪</a> </li> <li><a href="images/fbb.jpg" title="《还珠格格》,《武媚娘传奇》,《我不是潘金莲》..." >范冰冰</a></li> </ul> <div class="pic"> <img src="images/zwt.png" alt="" id="img"> </div> <p id='info'></p> </div> <script> var img1 = document.getElementsByTagName( 'a' ); //获取a标签 var pic = document.getElementById( 'img' ); //获取id为img的 var p = document.getElementById( 'info' ); //获取id为info的 for (var i=0; i<img1.length; i++) { //遍历 img1[i].onclick= function () { //img添加属性 for (var i = 0; i < img1.length; i++) { //遍历 img1[i].classList.remove('active'); //更新属性 } this.classList.add('active'); //增加 var picUrl = this.href; // 变量 获取a标签的href内容 this为当前 var picInfo = this.title; // 变量 获取a标签的title内容 this为当前 var picName = this.innerHTML; //// 变量 获取a标签的内容 innerhtml //将对应的图像与信息占位符进行替换 img.src = picUrl; p.innerHTML = '<span style="color:coral">'+picName+':'+picInfo+'</span>'; return false; }; } </script> </body> </html>
运行实例 »
点击 "运行实例" 按钮查看在线实例
实例
<!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; } .box .pic { width: 450px; height:450px; border: 1px solid lightgray; margin: 50px auto 0; } .box .pic img { width: 100%; height: 100%; } </style> </head> <body> <!-- 知识点: 1.js代码可以写在哪里?2.js如何调用的? 3.js函数的使用语法--> <div class="box"> <h2>明星相册</h2> <ul> <!--<li><a href="images/zly.jpg" onclick="document.getElementById('img').src = this.href;return false">赵丽颖</a></li>--> <li> <a href="images/zly.jpg" title="《楚乔传》,《花千骨》,《陆贞传奇》..">赵丽颖</a> </li> <li> <a href="images/gyy.jpg" title="《倚天屠龙记》,《咱们经婚吧》,《爱无悔》...">高圆圆</a> </li> <li> <a href="images/sl.jpg" title="《那年花开月正圆》,《甑环传》,《玉观音》..." >孙俪</a> </li> <li><a href="images/fbb.jpg" title="《还珠格格》,《武媚娘传奇》,《我不是潘金莲》..." >范冰冰</a></li> </ul> <div class="pic"> <img src="images/zwt.png" alt="" id="img"> </div> <p id='info'></p> </div> <script> var img1 = document.getElementsByTagName( 'a' ); var pic = document.getElementById( 'img' ); var p = document.getElementById( 'info' ); for (var i=0; i<img1.length; i++) { img1[i].onclick= function () { for (var i = 0; i < img1.length; i++) { img1[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>
运行实例 »
点击 "运行实例" 按钮查看在线实例