Correction status:qualified
Teacher's comments:
js可谓是前端开发必不可少的一部分,与用户的交互功能肯定离不开js。不仅如此,js还可以做出很多有趣的功能。今天我就介绍一下如果用js做出一个网络相册和计算器。
一、网络相册
<!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: 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-line: none; } .box ul li a:hover{ font-size: 1.2em; background-color: coral; } .box .pic{ width: 450px; height: 470px; border:1px solid lightgray; line-height: 1px; margin:20px auto; } .box .pic img{ width: 100%; height: 100%; } </style> </head> <body> <div class="box"> <h2>网络相册</h2> <ul> <li> <a href="http://cloud.zhibo1314.com/zly.jpg" title="《楚乔传》,《花千骨》,《蜀山战纪》" onclick="changePic(this);return false;">赵丽颖</a> </li> <li> <a href="http://cloud.zhibo1314.com/lss.jpg" title="《怪侠一枝梅》,《仙剑奇侠传三》,《轩辕剑之天之痕》" onclick="changePic(this);return false;">刘诗诗</a> </li> <li> <a href="http://cloud.zhibo1314.com/lyf.jpg" title="《天龙八部》,《神雕侠侣》,《仙剑奇侠传一》" onclick="changePic(this);return false;">刘亦菲</a> </li> <li> <a href="http://cloud.zhibo1314.com/wy.jpg" title="《余罪》,《飘香剑雨》,《睡在我上铺的兄弟》" onclick="changePic(this);return false;">吴优</a> </li> </ul> <div class="pic"> <img src="http://cloud.zhibo1314.com/zwt.png" id="img"> </div> <p id="info"></p> </div> <script type="text/javascript"> function changePic(pic){ var picUrl = pic.href var picInfo = pic.title var img = document.getElementById('img') var info = document.getElementById('info') img.src = picUrl info.innerHTML = '<span style="color:orange">'+'出演的电视剧:'+picInfo+'</span>' } </script> </body> </html>
点击 "运行实例" 按钮查看在线实例
运行结果:
二、计算器
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>迷你计算器</title> <style type="text/css"> .box{ width: 500px; height: 200px; background-color: #efefef; border:1px solid lightgray; margin:20px auto; text-align: center; color: #636363; box-shadow: 2px 2px 2px #999; } table{ margin:auto; } td{ width: 100px; height: 30px; padding: 5px 10px; } input,select{ width: 100%; height:100%; border: none; text-align: center; } button{ width: 100%; height:100%; border: none; background-color: skyblue; color: white; } button:hover{ background-color: coral; width: 105%; height: 105%; cursor: pointer; } </style> </head> <body> <div class="box"> <h2>迷你计算器</h2> <form> <table> <tr> <td><input type="text" name="opt1" placeholder="操作数1"></td> <td> <select name="option"> <option value="null">请选择操作</option> <option value="add">+</option> <option value="sub">-</option> <option value="mul">*</option> <option value="div">/</option> </select> </td> <td><input type="text" name="opt2" placeholder="操作数2"></td> <td><button type="button">计算</button></td> </tr> <tr> <td colspan="2" align="right"><h3>结果:</h3></td> <td colspan="2" align="left"><h3 id="placeholder"></h3></td> </tr> </table> </form> </div> <script type="text/javascript"> var opt1 = document.getElementsByName('opt1')[0] var opt2 = document.getElementsByName('opt2')[0] var opt = document.getElementsByName('option')[0] var btn = document.getElementsByTagName('button')[0] var placeholder = document.getElementById('placeholder') btn.onclick = function(){ if(opt1.value.length ==0){ alert('第一个操作数不能为空') opt1.focus() return false }else if(isNaN(opt1.value)){ alert('第一个操作数必须是数字') }else if(opt2.value.length ==0){ alert('第二个操作数不能为空') opt2.focus() return false }else if(isNaN(opt2.value)){ alert('第二个操作数必须是数字') }else{ var data1 = parseFloat(opt1.value) var data2 = parseFloat(opt2.value) } var option = opt.value var temp = 0 var flag = '' switch(option){ case 'null': alert('请选择操作符') opt.focus() return false case 'add': flag = '+' temp = data1+data2 break case 'sub': flag = '-' temp = data1-data2 break case 'mul': flag = '*' temp = data1*data2 break case 'div': flag = '/' if(data2 == 0){ alert('除数不能为0') return false }else{ temp = data1/data2 } break } placeholder.innerHTML = data1 + ''+ flag +'' + data2 + '=' + temp } </script> </body> </html>
点击 "运行实例" 按钮查看在线实例
运行结果:
手抄代码:
总结:
js获取元素可以通过getElementsByName,getElementsByTagName,getElementById等多种方式。