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

本文介绍了一种使用JavaScript和HTML实现输入框字数限制的方法。通过监听输入事件并在达到预设长度时截断字符串来确保输入内容不会超出限定长度。
 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>

转载于:https://www.cnblogs.com/mx2036/p/7099180.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值