????:<!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>微博輸入字?jǐn)?shù)</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('請(qǐng)輸入文字')
text.focus()//光標(biāo)自動(dòng)跑去text內(nèi)部
}else if(m<0){
alert('字?jǐn)?shù)過多')
text.focus()
}else{
alert('發(fā)布成功')
}
}
}
</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">長(zhǎng)微博</span>
<span id="sp6">公開</span>
<input type="button" value="發(fā)布" id="bt">
</div>
</body>
</html>
總結(jié):
1創(chuàng)建四個(gè)變量存儲(chǔ)數(shù)據(jù):text(輸入框),number(span提示),m(或許輸入框內(nèi)字符串長(zhǎng)度),bt(獲取button按鈕)
2點(diǎn)擊鍵盤判斷字?jǐn)?shù),如果鍵盤字?jǐn)?shù)過多輸出紅色,如果在140以內(nèi)輸出灰色
3點(diǎn)擊button判斷
?? ???:韋小寶?? ??:2019-02-15 15:25:45
???? ??:寫的很不錯(cuò) 總結(jié)的也很到位 沒事可以多寫點(diǎn)這種小案例 可以把之前學(xué)習(xí)的布局 以及js都練習(xí)練習(xí)!