在这个案例做完后,测试为空的时候。一点就成功发布。纠结死了。反复找问题,最后是m没初始化。只有键盘有响应了他才获得值。最后在前面给他初始化赋值150就没问题了。这个都不重要,重要的是这个问题是靠自己揪出来的。满满的收获。
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>微博输入</title>
<style>
body{font-size: 12px;}
.box{width: 600px;height: 180px; border:7px solid pink;margin:50px auto;}
.box img{float:left;margin-left: 10px;}
.box1{float:right;/* margin-right: 50px; */width: 150px;height: 24px;font-size:14px;color: #888;line-height: 24px;}
.box1 span{font-size: 16px;font-weight: bold;}
#text{width: 584px;height: 100px;border:1px solid #888;margin:5px;resize:none;padding: 5px}
.box .sp1{float: left;width: 30px;height: 32px;line-height: 32px;padding-left: 26px;}
#sp1{background: url(img/images1/an5.png) no-repeat left center;}
#sp2{background: url(img/images1/an4.png) no-repeat left center;}
#sp3{background: url(img/images1/an3.png) no-repeat left center;}
#sp4{background: url(img/images1/an2.png) no-repeat left center;}
#sp5{background: url(img/images1/an1.png) no-repeat left center;width: 40px;}
#sp6{margin-left: 150px;margin-right: 15px;color:#888;}
#bt{float:left;width: 80px;height: 30px;border: none;background: #ffc09f;color:#fff;border-radius: 5px;}
</style>
<script>
var text,number,m;
window.onload=function(){
text = document.getElementById('text');
number = document.getElementById('number');
bt = document.getElementById('bt');
m = 150;
text.onkeyup = function aa() {
m = 150-text.value.length
if (m<0) {
number.style.color="red";
}else{
number.style.color="#888";
}
number.innerHTML=m;
}
bt.onclick = function(){
if(m==150){
alert("内容不能为空!");
text.focus();//光标自动到textarea
}else if(m<0){
alert("字数超过限制,不能发布。")
text.focus();
}else{
alert("发布成功!")
}
}
}
</script>
</head>
<body>
<div>
<img src="img/images1/12.png" alt="">
<div>还可以输入:<span id="number">150</span>字</div>
<textarea id="text"onclick="aa()" placeholder="有新鲜事,记得分享给大家哦!"></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" id="bt" value="发布">
</div>
</body>
</html>