原生JavaScript( 五 )

18.点击有惊喜<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>Title</title><style>.box {width: 200px;height: 200px;background-color: red;border-radius: 50%;text-align: center;line-height: 200px;font-size: 25px;font-family: "Bitstream Vera Sans Mono", "DejaVu Sans Mono", Monaco, monospace;color: white;margin: 0 auto;}</style></head><body><div class="box">点击有惊喜</div><script>var oDiv = document.getElementsByClassName('box')[0];var count = 0;oDiv.onclick = function (){count++if (count == 1){this.style.backgroundColor = 'green'this.innerText = '继续点击'}else if (count == 2){this.style.backgroundColor = 'yellow'this.innerText = '精彩即将揭晓'}else if (count == 3){this.style.backgroundColor = 'pink'this.innerText = '骗你的傻逼'}else {this.style.backgroundColor = 'red'this.innerText = '点击有惊喜'count = 0}}</script></body></html>19.简易评论框<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>Title</title><style>.box1 {width: 100%;background-color: pink;text-align: center;font-size: 20px;word-break: break-all;}ul>li{list-style: none;text-align: left;/*height: 50px;*/width: 80%;background-color: #0e94ea;border: 1px dotted black;margin: 0 auto;margin-bottom: 10px;margin-left: 71px;border-radius: 10px;}.font {font-size: 15px;font-family: "Bitstream Vera Sans Mono", "DejaVu Sans Mono", Monaco, monospace;}</style></head><body><div class="box1">评论区<ul></ul></div><hr><div class="box2"><!--onblur: 失去焦点--><textarea cols="30" rows="10" id="content"onfocus="if(valuehttps://www.huyubaike.com/biancheng/=='留下你的脚印'){value=''}"onblur="if (valuehttps://www.huyubaike.com/biancheng/==''){value=''}">留下你的脚印</textarea></div><input type="button" value="https://www.huyubaike.com/biancheng/submit" id="btn"><input type="button" value="https://www.huyubaike.com/biancheng/count" id="cal"><script>var btn = document.getElementById('btn')btn.onclick = function (){var li = document.createElement('li')// 每一个楼层var text = document.getElementById("content")// 评论内容var val = text.valueif (val.length != 0){var ul = document.getElementsByTagName('ul')[0]var p1 = document.createElement('p')var p2 = document.createElement('p')p2.classList.add("font")// 设置时间和评论数量var count = document.getElementsByTagName('li').length + 1var ctime = new Date().toLocaleString()// 处理楼层内容p1.innerHTML = '#' + '<span class="num">'+ count + "</span>" + '楼' + '&nbsp;&nbsp;&nbsp;&nbsp;'+ ctime + '&nbsp;&nbsp;&nbsp;&nbsp;' + '<span class="del">删除</span>'// 处理评论内容p2.innerHTML = val// 添加到列表li中li.appendChild(p1)li.appendChild(p2)// 处理不存在文字ul.appendChild(li)text.valuehttps://www.huyubaike.com/biancheng/= ""var delButton = document.getElementsByClassName('del')var currentButton = delButton[delButton.length-1]// 获取到当前按钮// 每一个评论绑定点击的事件currentButton.onclick = function(){// alert(123123)// 把后面的楼层都减1var allNum = document.getElementsByClassName('num')// 拿到所有的楼层信息列表var currentNum = this.previousElementSibling// 核心操作: 将所有的num信息循环并找到当前点击的索引. 根据索引for (var i=0;i<allNum.length;i++){if (currentNum === allNum[i]){for(var j=i+1;j<allNum.length;j++){allNum[j].innerText = parseInt(allNum[j].innerText) -1;// 将num标签中的数字-1}}}ul.removeChild(this.parentNode.parentNode)// 删除li标签}}}</script></body></html>20.选项卡<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>Title</title><style>* {margin: 0;padding: 0;}.box {width: 600px;height: 400px;border: 1px solid red;margin: auto;}ul>li {list-style: none;float: left;width: 198px;height: 80px;background-color: gray;text-align: center;font-size: 30px;font-family: "Bitstream Vera Sans Mono", "DejaVu Sans Mono", Monaco, monospace;line-height: 80px;border: 1px solid white;}/*添加外墙. 防止父级塌陷*/ul:after {display: table;content: "";clear: both;}.content {width: 600px;height: 320px;background-color: pink;display: none;text-align: center;line-height: 320px;font-size: 50px;}div.active {display: block;}li.active {background-color: red;}</style></head><body><div class="box"><ul><li class="active">首页</li><li>双色球</li><li>大乐透</li></ul><div class="content active">这里是首页</div><div class="content">这里是双色球</div><div class="content">这里是大乐透</div></div><script>var arr = document.getElementsByTagName('li')for (var i=0;i<arr.length;i++){// 绑定点击事件并添加样式arr[i].n = iarr[i].onclick = function(){// 删除所有的样式for (var j=0;j<arr.length;j++){arr[j].classList.remove('active')document.getElementsByClassName('content')[j].classList.remove('active')}this.classList.add('active')document.getElementsByClassName('content')[this.n].classList.add('active')}}</script></body></html>

经验总结扩展阅读