摘要:<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>微博输入</title> <style> body{ font
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>微博输入</title>
<style>
body{
font-size: 15px;
}
.box{
width: 30%;
height: 240px;
margin:0 auto;
padding: 10px;
border: 1px solid #ccc;
}
img{
float: left;
}
.box #shuru{
float: left;
margin-left: 260px;
color: #888;
font-size: 15px;
}
.box #shuru span{
font-size: 16px;
font-weight: bold;
}
textarea{
width: 98%;
margin: 10px 2px;
}
.box #sp1,#sp2,#sp3,#sp4,#sp5,#sp6{
float: left;
width: 45px;
height: 32px;
line-height: 32px;
padding-left: 26px;
}
#sp1{background: url(images/an5.png) no-repeat no-repeat left center; }
#sp2{background: url(images/an4.png) no-repeat no-repeat left center; }
#sp3{background: url(images/an3.png) no-repeat no-repeat left center; }
#sp4{background: url(images/an2.png) no-repeat no-repeat left center; }
#sp5{background: url(images/an1.png) no-repeat no-repeat left center; }
#sp6{
margin-left: 60px;
color: #888;
}
#btn{float: left;width: 80px;height: 30px;boder:none;background: #ffc09f;color: #fff;border-radius: 5px;}
</style>
<script>
var text,number,m,bt;
window.onload = function () {
text = document.getElementById('text');
number = document.getElementById('number');
bt = document.getElementById('btn');
text.onkeyup = function () {
m = 140 - text.value.length;
if(m >= 0){
number.style.color = '#888';
}else{
number.style.color = 'red';
}
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 id="shuru" >还可以输入<span id="number"></span>字</div>
<textarea name="" id="text" cols="30" rows="10"></textarea>
<span id="sp1">表情</span>
<span id="sp2">图片</span>
<span id="sp3">视频</span>
<span id="sp4">话题</span>
<span id="sp5" style="width: 50px">长微博</span>
<span id="sp6">公开</span>
<input id="btn" type="button" value="发布">
</div>
</body>
</html>
总结:
1、字数提示的实现
![1551685196929210.png C~Q7V}YV7~E(JWY%C)Q]{1J.png](http://img.php.cn/upload/image/719/963/616/1551685196929210.png)
用到的事件和函数:
onkeyup: 键盘按键松开事件
innerHTML:往元素内部添加内容
2、发布提示
![1551685412485494.png EX_]_DP]U974_$GVQ$M6B}G.png](http://img.php.cn/upload/image/505/933/603/1551685412485494.png)
用到的事件和函数:
onclick:元素被点击的时候触发
alert:在浏览器上弹出一个弹框
focus:将焦点聚焦于指定的某个元素