<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>jquery自动监测输入字数,自动截取,输入框</title>
<script src="https://libs.baidu.com/jquery/1.9.1/jquery.min.js"></script>
</head>
<body>
<div class="box">
<textarea style="width: 500px;height: 250px" id="textarea"></textarea>
</div>
<div>
<div id="nobr">0</div>/300
</div>
</body>
<script>
$(document).on("keyup", "textarea", function() {
var data = $("#textarea").val().trim().replace(/\s+/g, "");
var count = data.length;
if (count > 300) {
var new_data = data.substring(0, 300);
$(".box").html(new_data);
$("#nobr").text(300);
} else {
$("#nobr").text(count);
}
})
</script>
</html>