微博输入字数实现

原创 2018-11-08 14:51:25 272
摘要:<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title></title> </head> <style> body{font-size: 12px;} .box{height: 160px;width: 600px

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title></title>

</head>

<style>

body{font-size: 12px;}

.box{height: 160px;width: 600px;border: 5px solid pink;padding:10px}

.box img{float: left;}

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

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

.box #text{height: 100px;width: 600px;}

.box #sp1,#sp2,#sp3,#sp4,#sp5,#sp6{float:left;height:32px;width: 30px;line-height: 32px;padding-left: 26px;}

.box #sp5{width: 40px;}

.box #sp6{margin-left:150px;margin-right: 20px;}

.box #sp1{background: url(../img/an5.png) no-repeat left center;}

.box #sp2{background: url(../img/an4.png) no-repeat left center;}

.box #sp3{background: url(../img/an3.png) no-repeat left center;}

.box #sp4{background: url(../img/an2.png) no-repeat left center;}

.box #sp5{background: url(../img/an1.png) no-repeat left center;}

.box input{float: left;width: 80px;height: 30px;border: none;background: #FFC09F;border-radius: 5px;color: #FFFFFF;}

</style>

<body>

<div class="box">

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

<div>还可以输入<span id="number">140</span>字</div>

<textarea id="text" onkeyup="enter()"></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="btn" onclick="submint()">

</div>

</body>

<script>

var text1,number1,m

window.onload = function(){

text1 = document.getElementById("text");

number1 = document.getElementById("number")

}

//输入字数判断

function enter(){

m = 140-text1.value.length

number1.innerHTML = m

if(m<0){

number1.style.color = "red"

}else{

number1.style.color = "#888"

}

}


//是否可以发布判断

function submint(){

if(m>=0){

alert("发布成功")

}else if(m<0){

text1.focus()

alert("已超出可输入字数")


}else{

text1.focus()

alert("输入不能为空")

}

}

</script>

</html>

  1. 函数的另一种方法   被调用元调用

  2. 素的id . 事件名 = function  函数名(){ }

  3. 改变元素内容   innerHTML

  4. 文本域获取焦点   text.focus()

批改老师:灭绝师太批改时间:2018-11-08 14:55:29
老师总结:完成的不错, 被调用元调用素的id . 事件名 = function { } 不要加函数名也ok,这个叫匿名函数

发布手记

热门词条