摘要:<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>微博输入框</title> <link rel="stylesheet" type="text/css&q
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>微博输入框</title>
<link rel="stylesheet" type="text/css" href="../css/css.css">
<link rel="shortcut icon" type="image/x-icon" href="../picture/mi.png">
<style type="text/css">
/*body内容里字体大小*/
body{font-size:12px;}
.box{width: 521px;height: 130px;border: 6px solid pink;margin: 5px auto;padding: 10px;}/*整个微博输大体框架*/
.box img{float: left;}
.box1{width: 100px;height: 24px;color: #ccc;font-size:12px;font-weight: bold;float: left;margin-left: 220px;line-height: 24px;text-align: right;}
/*.clear{clear: both;}*/
#number{font-size: 14px;font-weight: bold;}
#text{width: 521px;height: 70px;margin-top: 3px;}/*微博内容输入框*/
#sp1,#sp2,#sp3,#sp4,#sp5,#sp6{float: left; width: 30px;height: 25px;margin-top: 5px;padding-left: 26px; line-height: 25px;}/*通过浮动把行内span元素变成行内块元素
/*背景图片靠padding-left:26px寄出来*/
#sp1{background: url(../picture/images/an5.png) no-repeat left;}
#sp2{background: url(../picture/images/an4.png) no-repeat left;}
#sp3{background: url(../picture/images/an3.png) no-repeat left;}
#sp4{background: url(../picture/images/an2.png) no-repeat left;}
#sp5{background: url(../picture/images/an1.png) no-repeat left;width: 40px;}
#sp6{margin-left: 100px;}
#bt{width: 75px;height: 25px;margin-top: 5px;line-height: 25px;border: none;background: #FFC7A0;}/*发表按钮属性*/
</style>
</head>
<body>
<script type="text/javascript">
window.onload=function (){
var number,text,min;
number=document.getElementById("number");
text=document.getElementById("text");
bt=document.getElementById("bt");
text.focus();//页面加载完成后输入光标就在输入文本域
text.onkeyup=function myKeyup(){//该函数通过onkeyup() 事件统计输入字数和改变颜色
min=140-text.value.length;
if (min<0){
number.style.color="red";
}else{
number.style.color="#888";
}
number.innerHTML=min;//通过innerHTML改变字数
}
bt.onclick=function tiJiao(){//该函数判断发表是否成功
min=140-text.value.length;//通过mykeyup函数传给变量min然后给tiJiao函数使用
if (min==140){
alert("提交内容为空,请重新输入");
text.focus();//光标自动回到文本域
}else if (min<0){
alert("超出提交字数上线,请重新输入")
text.focus();
}else{
alert("发表成功");
}
}
}
</script>
<!-- 最外层Div -->
<div class="box">
<img src="../picture/images/12.png">
<!-- 计算输入字数的Div -->
<div class="box1">还可以输入<span id="number"></span>字</div>
<!-- <div class="clear"></div> -->
<!-- 清除浮动影响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>
通过该实例的练习是自己之前有点迷惑的只是点也明白了,还有就是我看老师讲的最后需要调用一个函数才能用,但是
我感觉tiJiao()这个函数其实只是需要用到mykeup()这个函数边变量min变动的值,
因为min是全局变量,所以只要把该变量引用到tiJiao()函数就可以了,没必要直接再把函数调用,不知道这样对不对?
运行结果是对的,还请老师给点意见!
批改老师:天蓬老师批改时间:2018-11-30 10:45:17
老师总结:其实,微博早已不再限制用户输入的字数了,但这仍是一个非常棒的练习项目, 检测用户输入的字数的同时, 其实还可以检测用户输入的内容,例如检测敏感词等