1 <!DOCTYPE html>
2 <html>
3 <head lang="en">
4 <meta charset="UTF-8">
5 <title>截取字符串的思想实现输入框字数限制</title>
6 </head>
7 <body>
8
9 <input id="t" oninput="text(this.value)"/>
10 <script>
11 function text(content) {
12 if (content.length > 5) {
13 document.getElementById("t").value =
14 document.getElementById("t").value.substring(0, 5);
15 }
16 }
17 </script>
18 </body>
19 </html>
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>截取字符串的思想实现输入框字数限制</title>
</head>
<body>
<input id="t" oninput="text(this.value)"/>
<script>
function text(content) {
if (content.length > 5) {
document.getElementById("t").value =
document.getElementById("t").value.substring(0, 5);
}
}
</script>
</body>
</html>