中间出错无数,常出错的地方:拼写错误,缺少标点,代码位置放错
相册实例--->电子产品报价单实例:
效果图
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>相册</title> <style type="text/css"> .price { width: 550px; height: 750px; margin:auto; text-align: center; background-color: lightgray; } .price h2 { width: 100%; height: 50px; line-height: 50px; color:black; margin:0; padding: 0; } .price ul { width: 100%; height: 50px; margin:0; padding: 0; overflow: hidden; } .price ul li { list-style-type: none; width:70px; height: 30px; float: left; margin:5px 15px; background-color: gray; border-radius:3px; box-shadow:1px 1px 3px 1px lightgray; } .price ul li a{ color:white; line-height: 30px; text-decoration-line:none; font-size: 0.9em; } .price ul li:hover { background-color: black; } .price img { width: 500px; height: 550px; margin: auto; background-color: black; border: 1px dotted gray; } .price p { text-align: left; margin-left:30px; } </style> </head> <body> <div class="price"> <h2>电子产品报价单</h2> <ul> <li> <a href="../images/ipad.png" title="2599" onclick="chPic(this);return false;">ipad</a> </li> <li> <a href="../images/ip6s.jpg" title="3599" onclick="chPic(this);return false;">iphone6s</a> </li> <li> <a href="../images/mac.png" title="7899" onclick="chPic(this);return false;">mac</a> </li> <li> <a href="../images/mate.png" title="5499" onclick="chPic(this);return false;">mate</a> </li> </ul> <hr> <img src="../images/ipx.png" alt="" id="pic"> <p id="priceInfo" >您所选择的产品价格为</p> </div> <script type="text/javascript"> function chPic(phone) { var phonePic = phone.href var phonePrice = phone.title var phoneName = phone.innerHTML // alert(phonePrice) var img = document.getElementById('pic') var p = document.getElementById('priceInfo') img.src = phonePic p.innerHTML = phoneName + '<span>价格为:</span>' + phonePrice } </script> </body> </html>
点击 "运行实例" 按钮查看在线实例
迷你计算器实例--->无格式丑陋的计算器
先不写样式了,以后有时间再改
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>计算器</title> </head> <body> <form> <table> <tr> <td> <input type="text" name="nub1" value="" placeholder="请输入第一个数字"> </td> <td> <select name="opt"> <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="nub2" value="" placeholder="请输入第二个数字"> </td> <td><button type="button">计算</button></td> </tr> <tr> <td colspan="2"><h3>结果:</h3></td> <td colspan="2"><h3 id="placeholder"></h3></td> <td></td> <td></td> </tr> </table> </form> <script type="text/javascript"> var nub1 = document.getElementsByName('nub1')[0] var nub2 = document.getElementsByName('nub2')[0] var math = document.getElementsByName('opt')[0] var btn = document.getElementsByTagName('button')[0] var placeholder = document.getElementById('placeholder') btn.onclick = function(){ if (nub1.value.length == 0) { alert('请填入第一个数字') nub1.focus() return false } else if (isNaN(nub1.value)){ alert('第一个数字:非法数据') nub1.focus() return false } else if (nub2.value.length == 0) { alert('请填入第二个数字') nub2.focus() return false } else if (isNaN(nub2.value)){ alert('第二个数字:非法数据') nub2.focus() return false } else { var date1 = parseFloat(nub1.value) var date2 = parseFloat(nub2.value) } var option = math.value var temp = 0 var flag = '' switch (option) { case 'null': alert('请选择计算方法') math.focus() return false case 'add': flag = '+' temp = date1 + date2 break case 'sub': flag = '-' temp = date1 - date2 break case 'mul': flag = '*' temp = date1 * date2 break case 'div': flag = '/' if (date2 == 0) { alert('除数不能为0') return false }else{ temp = date1 / date2 } break } var str = '<span style="color:red">' str += date1 + flag + date2+ '=' + temp str += '</span>' placeholder.innerHTML = str } </script> </body> </html>
点击 "运行实例" 按钮查看在线实例
手抄代码