我有这个代码
<div id="IIYBAGWNBC" contenteditable="true"></div>
这在 jquery 中
$("#IIYBAGWNBC").text(function(index, currentText) {
return currentText.substr(0, 100);
});
如何防止用户在 contenteditable
div
中输入超过 100 个字符
原文由 J.doe 发布,翻译遵循 CC BY-SA 4.0 许可协议
这很简单,在
keydown
上,计算元素字符串的长度并阻止用户尝试输入超过 100 个字符演示
解释:
On
keydown
orpaste
event on thecontenteditable
div
we check if thelength
of thediv
达到100
并且如果用户没有单击退格键而不是阻止用户通过单击任意键或什至通过右键单击粘贴来输入更多字符。