摘要:<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>微博输入布局</title> <style type="text/css"> body{background:#5DA8
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>微博输入布局</title> <style type="text/css"> body{background:#5DA8D3;font-size:12px;} .box{width:600px;height:170px;border:1px solid #ccc;background:#fff;margin:10px auto;} img{float:left;margin:5px 8px;} .wz{float:left;color:#808080;margin-left:315px;margin-top:12px;} #num{font-weight:bold;} #area{width:570px;height:80px;border:1px solid #ccc;margin:3px 10px;} .box #no1,#no2,#no3,#no4,#no5,#no6,#no7{float:left;width:30px;height:32px;line-height:32px;color:#000;padding-left:26px;} #no1{background:url(img/wb/01.png) no-repeat left center;margin-left:8px;} #no2{background:url(img/wb/02.png) no-repeat left center;} #no3{background:url(img/wb/03.png) no-repeat left center;} #no4{background:url(img/wb/04.png) no-repeat left center;} #no5{background:url(img/wb/05.png) no-repeat left center;width:48px;padding-left:30;} #no6{background:url(img/wb/06.png) no-repeat left center;margin-left:15px;} #no7{margin-left:70px;} input{border:1px solid #ccc;width:82px;height:30px;background:#FF8140;color:#fff;} </style> <script type="text/javascript"> var area,num,m//area是文本域输入的字数,num是右上方显示的字数,m是输入的字数 window.onload=function(){ area=document.getElementById('area')//获取这三个ID num=document.getElementById('num') bt=document.getElementById('bt') area.onkeyup=function aa(){//通过文本域的键盘弹起来判断 m=140-area.value.length//输入的字数m等于总字数140减去文本域已经输入字数的长度 if(m<0){//小于零,就是负数,证明输入超过了140个字 num.style.color="red"//字体颜色变红 }else{//不小于零,证明字数没有超过140个字 num.style.color="#888"//字体颜色正常 } num.innerHTML=m//把获取到的数直接输出到num位置 } bt.onclick=function(){//发布按钮点击事件来判断 if(m==140){//m等于140,证明输入字数为0 alert("您还没有输入") area.focus()//文本域获取焦点,更好的用户体验 }else if(m<0){//m小于0,就是负数,证明字数大于140个字 alert("字数太多,不可以发布") area.focus()/文本域获取焦点 }else{//字数正常,可以发布 alert("发布成功") } } aa()//新手注意:记得调用函数 } </script> </head> <body> <div class="box"> <img src="img/wb/00.png"> <div class="wz">已输入<span id="num"></span>字</div> <textarea id="area"></textarea> <span id="no1">表情</span> <span id="no2">图片</span> <span id="no3">视频</span> <span id="no4">话题</span> <span id="no5">头条文章</span> <span id="no6"></span> <span id="no7">公开</span> <input type="button" id="bt" value="发布"> </div> </body> </html>
其中的java部分对我来说还是有点难度,争取今天再多做两遍。
批改老师:天蓬老师批改时间:2018-12-30 13:37:20
老师总结:window.onload=function(){...}, 这个onload事件,以后实战中可以用DOMContentLoaded 这个事件来替换