微博输入的完整实例

原创 2019-03-04 15:47:11 235
摘要:<!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、字数提示的实现


C~Q7V}YV7~E(JWY%C)Q]{1J.png

用到的事件和函数:

onkeyup: 键盘按键松开事件

innerHTML:往元素内部添加内容

2、发布提示


EX_]_DP]U974_$GVQ$M6B}G.png

用到的事件和函数:

onclick:元素被点击的时候触发

alert:在浏览器上弹出一个弹框

focus:将焦点聚焦于指定的某个元素






批改老师:韦小宝批改时间:2019-03-04 15:49:01
老师总结:写的很棒 不错不错 这些事件在往后的开发中也会经常的使用到 课后没事记得多去练习练习哦

发布手记

热门词条