效果图如下:
以下是代码示例:
实例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>选项卡</title> <!-- 选项卡CSS --> <style type="text/css"> h2 { text-align: center; } .box { width: 500px; height:600px; border: 1px solid #ccc; margin: 20px auto; color: orange; } ul { margin: 0; padding: 0; background-color: #f8f8f8; overflow: hidden; } .box > ul li { list-style-type: none; width:90px; height:36px; text-align: center; line-height: 36px; float: left; border-right: 1px solid #ccc; border-bottom: 1px solid #ccc; } .box > span { float: right; width: 100px; height: 36px; line-height: 36px; margin-top: -36px; } .box span a { text-decoration: none; color: #696969; } .box li.active { background-color: #fff; font-weight: bolder; border-bottom: none; border-top: 3px solid orangered; } .contents > div { display:none; width:100%; height:550px; /*background-color: lightgreen;*/ /*border:1px solid red;*/ margin: 5px auto; text-align: center; } </style> <!-- 相册CSS --> <style type="text/css"> .mxxc { margin: 0; padding: 0; overflow: hidden; } .mxxc li { list-style-type: none; float:left; background-color: skyblue; margin-left: 20px; } .mxxc li a { /*将a转为块元素,并设置为li的宽高,这样可以使整个li可以被点击*/ display: block; width: 100px; height: 40px; line-height: 40px; color: white; text-decoration: none; } .mxxc li:hover { font-size:1.2em; background-color: coral; } .mxxc + .pic { width: 300px; height:350px; border: 1px solid lightgray; /*消除img标签底部的空间区*/ line-height: 1px; margin: auto; margin-top: 50px; } .mxxc + .pic img { width: 100%; height: 100%; } </style> <!-- 计算器CSS --> <style type="text/css"> table { margin: auto; } td { width: 150px; height: 30px; padding: 5px 10px; text-align: right; } input, select { width: 200px; height:100%; text-align: center; } select { width: 204px; } button { width: 100%; height: 100%; border: none; background-color: skyblue; color: white; } button:hover { cursor: pointer; background-color: coral; width: 105%; height: 105%; } </style> <!-- 客服CSS --> <style type="text/css"> .content:nth-child(3){ width: 450px; height: 530px; margin: 10px auto; border: 3px solid #fcc; color: #333; } .content:nth-child(3) div{ width: 100%; height: 430px; border-bottom: 2px solid #fcc; background-color: #efefef; margin: 0 auto; } .content:nth-child(3) div ul { line-height: 2em; text-align: left; } .content:nth-child(3) div ul li { background-color: #efefef; padding-left:10px; padding-top:5px; } .content:nth-child(3) div table { width: 100%; height:80px; margin:0 auto; } textarea{ width: 330px; height:80px; border: 1px solid #fcc; resize: none; background-color: lightyellow; } .content:nth-child(3) button { width: 60px; height: 40px; background-color: seagreen; color: white; border: none; } .content:nth-child(3) button:hover { cursor: pointer; background-color: orange; } </style> </head> <body> <h2>综合选项卡</h2> <div class="box"> <ul> <li class="active">明星相册</li> <li>计算器</li> <li>客服妹妹</li> </ul> <span><a href="">更多内容>></a></span> <div class="contents"> <div class="content" style="display: block"> <ul class="mxxc"> <li> <a href="images/zly.jpg" title="《楚乔传》,《花千骨》,《陆贞传奇》..." onclick="changePic(this);return false">赵丽颖</a> </li> <li> <a href="images/gyy.jpg" title="《倚天屠龙记》,《咱们经婚吧》,《爱无悔》..." onclick="changePic(this);return false">高圆圆</a> </li> <li> <a href="images/sl.jpg" title="《那年花开月正圆》,《甑环传》,《玉观音》..." onclick="changePic(this);return false">孙俪</a> </li> <li><a href="images/fbb.jpg" title="《还珠格格》,《武媚娘传奇》,《我不是潘金莲》..." onclick="changePic(this);return false">范冰冰</a></li> </ul> <div class="pic"> <img src="images/zwt.png" alt="" id="img"> </div> <br> <p id='info'></p> </div> <div class="content"> <h2>HIV cutoff值计算</h2> <form> <table> <tr> <td>请选择试剂厂商:</td> <td> <select name="factory" align:center> <option value="null">请选择厂商</option> <option value="bjwt"> 北京万泰 </option> <option value="zhlz"> 珠海丽珠 </option> <option value="shkh"> 上海科华 </option> </select> </td> </tr> <tr> <td>数值1:</td> <td><input type="text" name="num1" placeholder=""></td> </tr> <tr> <td>数值2:</td> <td><input type="text" name="num2" placeholder=""></td> </tr> <tr> <td>数值3:</td> <td><input type="text" name="num3" placeholder="科华试剂无需输入"></td> </tr> <tr> <td colspan="2"><button type="button">计算</button></td> </tr> <tr> <td>cutoff值:</td> <td><h3 id="placeholder"></h3></td> </tr> </table> </form> </div> <div class="content"> <div contenteditable="true"> <ul> </ul> </div> <table> <tr> <td align="right"><textarea cols="50" rows="4" name="text"></textarea></td> <td align="left"><button type=button>发送</button></td> </tr> </table> </div> </div> </div> <!-- 选项卡JS脚本 --> <script type="text/javascript"> var box = document.getElementsByClassName('box')[0] var ul = box.getElementsByTagName('ul')[0] var li = ul.getElementsByTagName('li') var content = document.getElementsByClassName('contents')[0] var list = content.getElementsByClassName('content') for (var i=0; i<list.length; i++) { li[i].index = i li[i].onmouseover = function (){ for (var i = 0; i < li.length; i++) { li[i].className = '' list[i].style.display = 'none' } this.className = 'active' list[this.index].style.display = 'block' } } </script> <!-- 明星相册JS脚本 --> <script type="text/javascript"> function changePic(pic) { //1.获取到要替换的明星图片与简介信息 var picUrl = pic.href var picInfo = pic.title var picName = pic.innerHTML //2.获取到页面中,要被替换掉的图像元素对象 var img = document.getElementById('img') var p = document.getElementById('info') //3. 将对应的图像与信息占位符进行替换 img.src = picUrl p.innerHTML = picName+':'+picInfo p.innerHTML = '<span style="color:coral">'+picName+':'+picInfo+'</span>' } </script> <!-- 计算器JS脚本 --> <script type="text/javascript"> //1.获取操作数,按钮与结果占位符 var num1 = document.getElementsByName('num1')[0] var num2 = document.getElementsByName('num2')[0] var num3 = document.getElementsByName('num3')[0] var fac = document.getElementsByName('factory')[0] var btn = document.getElementsByTagName('button')[0] var placeholder = document.getElementById('placeholder') //2.给按钮添加事件,执行计算操作 btn.onclick = function(){ var factory = fac.value var temp = 0 switch (factory){ case 'null': alert("请选择试剂厂商") fac.focus() return false } if (num1.value.length == 0 ) { alert('数值1不能为空') num1.focus() return false } else if (isNaN(num1.value)) { alert('数值1必须为数字') num1.focus() return false } else if (num2.value.length == 0) { alert('数值2不能为空') num2.focus() return false } else if (isNaN(num2.value)) { alert('数值2必须为数字') num2.focus() return false } else if (num3.value.length == 0) { //当选择"上海科华时"第三个数无需输入 switch (factory){ case 'bjwt': alert('数值3不能为空') return false case 'zhlz': alert('数值3不能为空') return false } } else if (isNaN(num3.value)) { alert('数值3必须为数字') } { var data1 = parseFloat(num1.value) var data2 = parseFloat(num2.value) var data3 = parseFloat(num3.value) } //定义不同试剂厂商的计算公式 switch (factory){ case 'null': alert("请选择试剂厂商") fac.focus() return false case 'bjwt': temp = (data1 + data2 + data3)/3+0.12 break case 'zhlz': temp = (data1 + data2 + data3)/3+0.15 break case 'shkh': temp = (data1 + data2 )/2*0.1 } var str = '<span style="color:coral">' str += temp str += '</span>' placeholder.innerHTML = str } </script> <!-- 客服JS脚本 --> <script type="text/javascript"> //获取到页面中的按钮,文本域,对话内容区 var send = document.getElementsByTagName('button')[1] var text = document.getElementsByName('text')[0] var mess = document.getElementsByTagName('ul')[2] var sum = 0 send.onclick = function (){ //检测用户输入信息,如果为空弹出提示信息 if (text.value.length == 0) { alert('客官:是不是忘记输入内容了~~') return false } var userComment = text.value text.value = '' //创建一个'li' var addli = document.createElement('li') addli.innerHTML = userComment var userPic = '<img src="images/peter.jpg" width="30" style="border-radius:50%">' addli.innerHTML = userPic+userComment sum += 1 //这里我与朱老师的课程中讲的源码有所不同,在这里先判断窗口中的聊天条数,超过限制先清屏再输出输入内容 if (sum > 8) { mess.innerHTML = '' sum = 0 } //在mess即'ul'中使用函数动态添加一个'li' mess.appendChild(addli) setTimeout(function(){ var info = ['你好烦人呀,本姑娘好累,不知道怜香惜玉吗?','除了退货,退款,维修,什么问题都可以问','啥事呀,我的帅哥哥'] var temp = info[Math.floor(Math.random()*3)] //取1-5之间的一个整数:Math.floor(Math.random()*6 + 1) var reply = document.createElement('li') var kefuPic = '<img src="images/zly.jpg" width="30" style="border-radius:50%;">' // reply.innerHTML = '你有啥事呀,我的帅哥哥' +kefuPic reply.innerHTML = kefuPic + '<span style="color:red">'+temp+'</span>' // reply.style.float = 'right' sum += 1 if (sum > 8) { mess.innerHTML = '' sum = 0 } mess.appendChild(reply) },2000) } </script> </body> </html>
运行实例 »
点击 "运行实例" 按钮查看在线实例
总结:
此次案例将选项卡功能、相册功能、计算器功能、聊天以及回复功能融合在一起,如果分单个来写相对来说简单一些,放在一起时使用CSS/JS需要注意各个区块的命名规范,渲染页面时需要注意CSS选择器选择制定的模块,JS获取元素时也应该注意该元素在整个文档流中的位置,正确使用获取函数。
手抄JS脚本:
计算器:
聊天机器人: