textarea文字域輸入字數實時控制
阿新 • • 發佈:2018-12-12
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>實時控制字數</title>
</head>
<body>
</body>
<script src="jquery-3.2.0.min.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">
function numTip() {
$("#currentNum").text(100 - $("#textArea").val().length);
if($("#textArea").val().length >= 90) {
$("#currentNum").css("color", 'red');
} else {
$("#currentNum").css("color", 'black');
}
}
</script>
</html>
<html>
<head>
<meta charset="UTF-8">
<title>實時控制字數</title>
<style type="text/css">
* { padding: 0; margin: 0; } .box { width: 400px; height: 300px; margin: 20px auto; } .box textarea { width: 400px; height: 270px; font-size: 20px; } </style>
</head>
<body>
<div class="box">
<textarea name="" onkeyup="numTip()" maxlength="100" id="textArea"></textarea>
<p dir="rtl"><span id="currentNum">100</span>/100</p>
</div>
</body>
<script src="jquery-3.2.0.min.js" type="text/javascript" charset="utf-8"></script>
function numTip() {
$("#currentNum").text(100 - $("#textArea").val().length);
if($("#textArea").val().length >= 90) {
$("#currentNum").css("color", 'red');
} else {
$("#currentNum").css("color", 'black');
}
}
</script>
</html>