サマリー:<!DOCTYPE html><html><head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
<!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>我的作業(yè)</title>
<style type="text/css">
body{font-size: 13px;}
.box{width: 600px;height: 160px;border: 10px solid pink;margin: 0px auto;padding: 10px;}
/* div的樣式邊框以及內(nèi)外邊距,內(nèi)邊距會(huì)把元素向外撐大 */
img{float: left;}
.box1{float: left;margin-left: 255px;width: 150px;height: 24px;text-align: right;font-size: 14px;color: #888;}
/* 里面兩個(gè)元素 img與文本span 設(shè)置左浮動(dòng)變?yōu)閴K級(jí)元素,然后設(shè)置左外邊距,固定位置 */
.box1 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: 30px;height: 32px;line-height: 32px;padding-left: 26px;}
/* 設(shè)置元素左浮動(dòng)變?yōu)閴K級(jí)元素,然后line-height設(shè)置塊級(jí)元素的行高,內(nèi)左邊距為26 */
#sp1{background: url(img/an5.png)no-repeat left center;}
#sp2{background: url(img/an4.png)no-repeat left center;}
#sp3{background: url(img/an3.png)no-repeat left center;}
#sp4{background: url(img/an2.png)no-repeat left center;}
#sp5{background: url(img/an1.png)no-repeat left center;width: 40px;}
/* 設(shè)置元素的圖片不平鋪 向左 居中*/
#sp6{margin-left: 160px;margin-right: 15px;color: #888;}
#bt{float: left;width: 75px;height: 30px;border: none;background: #ffc09f;color:#fff;border-radius: 5px;}
</style>
<script type="text/javascript">
window.onload=function(){
var text=document.getElementById('text')
var number=document.getElementById('number')
bt=document.getElementById('bt')
text.onkeyup=function aa(){
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)輸入內(nèi)容")
text.focus()
}
else if(m<0)
{
alert("字?jǐn)?shù)太多,請(qǐng)重新輸入")
text.focus()
}
else
{
alert("發(fā)布成功")
}
}
aa()
}
</script>
</head>
<body>
<div class="box">
<img src="img/12.png" alt="">
<div class="box1">還可以輸入 <span id="number"></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" id="bt" value="發(fā)布">
</div>
</body>
</html>
我有一個(gè)問題 script里 有4個(gè)變量, munber text m bt 如果都不用var聲明 程序也是可以正常運(yùn)行的 但是如果聲明了bt 點(diǎn)擊button按鈕時(shí)就沒有反應(yīng),聲明了m 根據(jù)作用域不同 也會(huì)出問題 這是為什么呢
添削の先生:西門大官人添削時(shí)間:2019-03-03 14:19:31
先生のまとめ:用var 聲明bt,不會(huì)出現(xiàn)點(diǎn)擊沒反應(yīng)的情況。
程序里沒有用var 聲明m,那么m就是一個(gè)在方法中定義的全局變量,效果和在外面定義的其他4個(gè)全局變量一樣,在不同的方法里這個(gè)變量是共享的,即:都可以使用。如果在方法里用var m 這樣定義了m,那么這個(gè)m只在本方法里有效。其他方法里無法使用