相册:
实例 <html lang="en"> <head> <meta charset="UTF-8"> <title>2.实战:明星相册</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: red; text-decoration: none; } .box ul li:hover { font-size:1.2em; background-color: coral; } .box .pic { width: 450px; height:450px; border: 1px solid lightgray; line-height: 1px; margin: auto; margin-top: 50px; } .box .pic img { width: 100%; height: 100%; } .box .pic img:hover { } </style> </head> <body> <div class="box"> <h2>明星相册</h2> <ul> <li> <a href="../images/zly.jpg" title="《楚乔传》,《花千骨》,《陆贞传奇》..." onclick="changePic(this);return false">赵丽颖</a> </li> <li> <a href="https://ss2.baidu.com/6ONYsjip0QIZ8tyhnq/it/u=3802769585,1517263243&fm=58" title="《仙剑奇侠传》,《神话》..." onclick="changePic(this);return false">胡歌</a> </li> <li> <a href="https://ss2.baidu.com/6ONYsjip0QIZ8tyhnq/it/u=3376675274,1867520384&fm=58&w=121&h=140&img.JPEG&bpow=400&bpoh=613" title="《枪王之王》,《使徒行者》.." onclick="changePic(this);return false">古天乐</a> </li> </ul> <div class="pic"> <img src="../images/zwt.png" alt="" id="img"> </div> <p id='info'></p> </div> <script type="text/javascript"> function changePic(pic) { var picUrl = pic.href var picInfo = pic.title var picName = pic.innerHTML var img = document.getElementById('img') var p = document.getElementById('info') img.src = picUrl p.innerHTML = picName+':'+picInfo p.innerHTML = '<span style="color:coral">'+picName+':'+picInfo+'</span>' } </script> </body> </html> 运行实例 » 点击 "运行实例" 按钮查看在线实例
计算器:
<html> <head> <title>计算器</title> </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"> //Js 获取html 里面的name 值熟悉 并且取属猪里的第一个 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 ) { //判断 选项框1 是否为空值 alert('第一个操作数不能为空') opt1.focus() return false //程序截止 } else if (isNaN(opt1.value)) { //判断值是否不是数字 alert('第一个操作数必须为数字') opt1.focus() return false } else if (opt2.value.length == 0) { alert('第二个操作数不能为空') opt2.focus() return false } else if (isNaN(opt2.value)) { alert('第二个操作数必须为数字') opt2.focus() return false } else { var data1 = parseFloat(opt1.value) //字符串转数字 var data2 = parseFloat(opt2.value) } var option = opt.value //选项框值 var temp = 0 var flag = '' //先设置为空 var result = '' switch (option){ // switch 判断 其中的一样case 是以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,请重新输入') opt2.focus() return false } else { temp = data1 / data2 } break } var str = '<span style="color:red">' //输出结果 str += data1+' '+flag+' '+data2 + ' = ' + temp str += '</span>' placeholder.innerHTML = str } </script> </body> </html>
点击 "运行实例" 按钮查看在线实例