<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>My Practice</title>
<script src="jquery-3.2.1.js"></script>
</head>
<body>
<div>
<textarea onkeyup="javascript:checkWord(this);" onmousedown="" ></textarea>(<span id="ResidueCount">10</span>)
</div>
<span>test</span>
<script type="text/javascript">
var str = "";
var maxLength = 10;
function checkWord(c) {
str = c.value;
//console.log(str + " " + str.length);
var len = maxLength - str.length;
if (len >= 0) {
$("#ResidueCount").text(len);
}
else {
c.value = str.substring(0, maxLength);
}
}
</script>
</body>
</html>
参考:http://www.studyofnet.com/news/529.html