微博输入字数

Original 2019-02-15 15:14:42 201
abstract:<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0">&

<!DOCTYPE html>

<html lang="en">

<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>

*{margin:0;padding: 0;}

.box{width: 600px;height: 160px;border: 10px solid orange;margin: 0 auto;padding: 10px;}

img{float: left;}

.box1{float: left;margin-left: 255px;width: 150px;height: 24px;text-align: right;font-size: 14px;color: #888;}

.box span{font-size: 16px;font-weight: bold;}

#text{width: 600px;height: 100px;border: 1px solid #888;margin-top: 5px;}

.box #sp1,#sp2,#sp3,#sp4,#sp5,#sp6{float:left;width: 39px;height: 32px;line-height: 32px;padding-left: 20px;font-size: 10px;}

#sp6{margin-left: 150px;margin-right: 15px;color:#888;}

#bt{float: left;width: 80px;height: 30px;border: none;background: #ffc09f;color: #fff;border-radius: 4px;}

</style>

</head>

<body>

<script>

var text,number,m,bt

window.onload=function(){

text=document.getElementById('text')

number=document.getElementById('number')

bt=document.getElementById('bt')

text.onkeyup=function keyup(){

m=140-text.value.length

if(m<0){

number.style.color='red';

}else{

number.style.color='#888';

}

number.innerHTML=m

}

bt.onclick=function(){

if(m==140){

alert('请输入文字')

text.focus()//光标自动跑去text内部

}else if(m<0){

alert('字数过多')

text.focus()

}else{

alert('发布成功')

}

}

}

</script>

<div class="box">

<img src="../images/12.png">

<div class="box1">还可以输入<span id='number'>140</span>字</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>

总结:

1创建四个变量存储数据:text(输入框),number(span提示),m(或许输入框内字符串长度),bt(获取button按钮)

2点击键盘判断字数,如果键盘字数过多输出红色,如果在140以内输出灰色

3点击button判断


Correcting teacher:韦小宝Correction time:2019-02-15 15:25:45
Teacher's summary:写的很不错 总结的也很到位 没事可以多写点这种小案例 可以把之前学习的布局 以及js都练习练习!

Release Notes

Popular Entries