有以下任务 - 我需要使用本机JS / JQuery防止在contenteditable div中输入超过N个符号,即用户输入10个符号并且不能输入更多,只能清除。
<div contenteditable="true" placeholder="Текст сообщения" class="editable ng-valid ng-valid-maxlength ng-dirty ng-valid-parse ng-touched"></div>
答案 0 :(得分:0)
这应该适合你。小提琴是answer
<execution-context xmlns="http://www.vmware.com/vco">
<parameters>
<parameter name="host" type="CHEF:ChefHost">
<sdk-object type="Chef:Host" id=<Enter host id>/>
</parameter>
</parameters>
$('#editcontent').on('keydown', function(event) {
$('span').text('Characters entered:' + $(this).text().length);
if ($(this).text().length === 10 && event.keyCode != 8) /*delete*/ {
event.preventDefault();
}
});
body {
padding: 10px 0 0 10px;
}
#editcontent {
width: 200px;
height: 50px;
border: 1px solid blue;
}
#editcontent:after {
color: #999;
content: 'Enter text';
}