abstract:基本思路 在上一个作业中在微博HTML中创立好基本样式之后,需要在JavaScript中加入相关的逻辑功能从而把微博输入字数的所有功能实现。(1)首先在script标签内声明微博输入字数的几个变量,并通过id在相关标签内扎搜到函数,给一个某键盘按键被松开的事件,其作用是在输入字数时左上角会有字数提示,在输入超过限制字数140字时会变成红色的字体;&nb
基本思路
在上一个作业中在微博HTML中创立好基本样式之后,需要在JavaScript中加入相关的逻辑功能从而把微博输入字数的所有功能实现。
(1)首先在script标签内声明微博输入字数的几个变量,并通过id在相关标签内扎搜到函数,给一个某键盘按键被松开的事件,其作用是在输入字数时左上角会有字数提示,在输入超过限制字数140字时会变成红色的字体;
var text,number,button,m window.onload=function (){ text=document.getElementById('text') number=document.getElementById('number') button=document.getElementById('button') text.onkeyup=function aa(){ m=140-text.value.length//微博限制字数是140 if(m<0){ number.style.color="red" }else{ number.style.color="#888" } number.innerHTML=m; }
(2)之后对发布框的id为“button”的标签给一点击此对象调用的事件,使用户写完微博后点击发布按钮时会出现相应的提示信息。
button.onclick=function(){ if (m==140) { alert("您还没有输入") text.focus() }else if(m<0){ alert("字数太多,不可以发布") text.focus() }else{ alert("发布成功") } } aa()
注意事项:
(1)右上角的字数提示信息内,其写法为“m=140-text.value.length”,需要在text后加上value而不是直接加上length;
(2)在点击发布按钮时,如若发微博的字数不在要求范围内,应在相关逻辑下加上“text.focus()”,使提示框后焦点自动在输入文本框最前面。
完整代码:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>微博输入练习</title> <style type="text/css"> body{font-size: 12px;} body a{text-decoration: none; color: #000;} a:hover{color: #888;text-decoration: underline;} .box{width: 600px;height: 160px;border:10px solid #C3C6FF; margin: 0px auto;padding:10px;border-radius: 7px; } img{float: left;} .box1{float: left;margin-left: 255px;width: 150px;height: 24px;text-align: right;font-size: 14px;color: #888;} .box1 span{font-size: 16px;font-weight: bold;} #text{width:600px;height: 100px;border: 1px solid #ccc;margin-top: 5px;font-size: 18px;font-family: 黑体;} .box #a1,#a2,#a3,#a4,#a5,#a6{float: left;width: 30px;height: 32px;line-height: 32px;padding-left: 26px;} #a1{background: url(images/an5.png) no-repeat left center;} #a2{background: url(images/an4.png) no-repeat left center;} #a3{background: url(images/an3.png) no-repeat left center;} #a4{background: url(images/an2.png) no-repeat left center;} #a5{background: url(images/an1.png) no-repeat left center;width: 40px;} #a6{ margin-left: 158px;margin-right:15px;color: #888; } #button{float: right;width: 80px;height: 30px;border: none;background: #ffc09f;color: #fff;border-radius: 5px;} </style> <script type="text/javascript"> var text,number,button,m window.onload=function (){ text=document.getElementById('text') number=document.getElementById('number') button=document.getElementById('button') text.onkeyup=function aa(){ m=140-text.value.length//微博限制字数是140 if(m<0){ number.style.color="red" }else{ number.style.color="#888" } number.innerHTML=m; } button.onclick=function(){ if (m==140) { alert("您还没有输入") text.focus() }else if(m<0){ alert("字数太多,不可以发布") text.focus() }else{ alert("发布成功") } } aa() } </script> </head> <body> <div class="box"> <img src="images/12.png"> <div class="box1">还可以输入<span id="number"></span>字</div> <textarea id="text"></textarea> <input type="button" value="发布" id="button"> <a href="#" id="a1">表情</a> <a href="#" id="a2">图片</a> <a href="#" id="a3">视频</a> <a href="#" id="a4">话题</a> <a href="#" id="a5">长微博</a> <a href="#" id="a6">公开</a> </div> </body> </html>
END
Correcting teacher:灭绝师太Correction time:2018-11-19 17:43:12
Teacher's summary:完成的不错,主要理解的很好,理解比完成更重要……加油,尝试不同的案例