代码改变世界

54.截取字符串的思想实现输入框字数限制

2017-06-30 15:42  笨笨03  阅读(179)  评论(0编辑  收藏  举报
 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>
View Code

 

<!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>