我想将可编辑div中的字符数限制为10.在用户达到限制后,我想使用.preventDefault()方法来保护div免受其他字符的影响.你能救我吗?JSFiddle https://jsfiddle.net/7x2dfgx6/1/ HTML
<div class="profileInfo" id="About" style="border:solid;" contenteditable="true">OOOOO</div>
Run Code Online (Sandbox Code Playgroud)
JQuery的
jQuery(document).ready(function($){
const limit = 10;
rem = limit - $('#About').text().length;
$("#counter").append("You have <strong>" + rem + "</strong> chars left.");
$("#About").on('input', function(event) {
var char = $('#About').text().length;
rem = limit - char;
$("#counter").html("You have <strong>" + rem + "</strong> chars left.");
if(char>limit)
{
event.preventDefault();
//TODO
}
});
});
Run Code Online (Sandbox Code Playgroud)