摘要:<!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <title>微博发布</title> &nbs
<!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <title>微博发布</title> <style type="text/css"> body{font-size:12px;}/* 设置所有的字体大小 */ .box{margin:0px auto;border:8px solid pink;width:600px;height:160px;padding:10px;}/* 设置位置边框和大小 */ .box img{float:left;height:24px;} .boxtext{float:left;margin-left:280px;font-size:15px;} #text{width:600px;height:100px;margin-top:8px;border:1px solid #888;} .box #sp1,#sp2,#sp3,#sp4,#sp5,#sp6{float:left;width:30px;height:32px;line-height:32px;padding-left:28px;} #sp1{background:url(images/an5.png) no-repeat left center;}/* 设置表情的图标 */ #sp2{background:url(images/an4.png) no-repeat left center;}/* 设置图片的图标 */ #sp3{background:url(images/an3.png) no-repeat left center;}/* 设置视频的图标 */ #sp4{background:url(images/an2.png) no-repeat left center;}/* 设置话题的图标 */ #sp5{width:40px;background:url(images/an1.png) no-repeat left center;}/* 设置长微博的图标 */ #sp6{margin-left:150px;margin-right:10px;} /* 设置公开的位置 */ #bt{border:none; background:#ffc09f;float:left;width:80px;height:28px;color:#fff;border-radius:5px;}/* 设置发布按钮样式 */ </style> <script type="text/javascript"> var text,number,m,bt window.onload = function(){//加载完成后执行此函数 text=document.getElementById('text') number=document.getElementById('number') bt=document.getElementById('bt') text.onkeydown = function aa(){//键盘弹起操作和按下的事件都可 m=140-text.value.length//设置微博字符不超过140个字符 if(m<0){ number.style.color="red" }else{ number.style.color="#888" } number.innerHTML=m bt.onclick= function(){ if(m==140){ alert('您还没有输入字符') text.focus(); }else if(m<0){ alert('您输入的字符太多') text.focus(); }else{ alert(' 发布成功') } } } } </script> </head> <body> <div class="box"> <img src="images/12.png" alt="" /><!--设置图片,大小高度以图片的为准--> <div class="boxtext">还可以输入<span id="number"></span>字</div> <textarea id="text"></textarea> <span id="sp1">表情</span> <span id="sp2">图片</span> <span id="sp3">视频</span> <span id="sp4">话题</span> <span id="sp5">长微博</span> <span id="sp6">公开</span> <input type="button" value="发布" id="bt" /> </div> </body> </html>
onkeyup和onkeydown在这里都可以用,还有最后在执行函数aa()可以省略,window.load在这里很重要。
批改老师:灭绝师太批改时间:2018-11-19 14:17:17
老师总结:恩恩是的,很重要,每一个事件方法都要牢牢记住,都有应用到他们的场景