摘要:<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>案例-微博输入框</title> <style type="text/css"> body{font-size:&n
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>案例-微博输入框</title>
<style type="text/css">
body{font-size: 12px;}
.box{width: 600px;height: 160px;border: 10px solid pink;margin: 0px auto;padding: 10px;}
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 #888;margin-top: 5px;}
.box #span1,#span2,#span3,#span4,#span5,#span6{float: left;width: 30px;height: 32px;line-height: 32px;padding-left: 26px;}
#span1{background: url(images/an5.png) no-repeat left center;}
#span2{background: url(images/an4.png) no-repeat left center;}
#span3{background: url(images/an3.png) no-repeat left center;}
#span4{background: url(images/an2.png) no-repeat left center;}
#span5{background: url(images/an1.png) no-repeat left center; width: 40px;}
#span6{margin-left:150px;margin-right: 15px;color: #888;}
#submit{float: left;width: 80px;height: 30px;border: none;background:#ffc09f;color: #fff;border-radius: 5px;}
</style>
<script type="text/javascript">
window.onload=function(){
var text=document.getElementById("text");
var number=document.getElementById("number");
//var maxlength=140; //最大输入值
//var length=text.value.length;
var submit=document.getElementById('submit');
text.onkeyup=function show(){
count=140-text.value.length; //微博限制字数为140
//count=maxlength-length;
if (count<0) {
text.style.color="red"
alert("字数过多")
} else {
text.style.color="#888"
}
number.innerHTML=count
}
submit.onclick=function(){
if (count==140) {
alert("请输入内容");
text.focus()
} else if(count<0){
alert("字数过多");
text.focus()
}else{
alert("发布成功")
}
}
show()
}
</script>
</head>
<body>
<div class="box">
<img src="images/12.png">
<div class="box1">还可以输入<span id="number"></span>字</div>
<!-- <textarea id="text"></textarea> -->
<textarea id="text"></textarea>
<span id="span1">表情</span>
<span id="span2">图片</span>
<span id="span3">视频</span>
<span id="span4">话题</span>
<span id="span5">长微博</span>
<span id="span6">公开</span>
<input type="button" value="发布" id="submit">
</div>
</body>
</html>字数过多的时候弹框提示字数过多,并且输入框内容变红。
对于限制字数那块,我原本想使用maxlength-length来代替,但是不行,140不会改变;
count=140-text.value.length; //这样就ok
count=maxlength-length; //这样就不行,我分别定义了maxlength和length,最终结果那个值始终是140,不会改变,我想是因为我定义了maxlength的原因,但是maxlength=140,然后maxlength-,和直接使用140-,有什么区别?
还有我想要实现不点击输入框,不显示“还可以输入140个字”,点击输入框之后才显示,使用了onmousedown,onclick,onkeyup都不行...
批改老师:天蓬老师批改时间:2018-11-02 20:54:43
老师总结:想要实现你说的功能,可以有onchange事件