2016-05-25 163 views
0

CONTENTEDITABLE DIV有以下任務 - 我需要防止在CONTENTEDITABLE DIV比N個符號的輸入更多與天然JS/JQuery的,即用戶輸入10碼元和不能輸入多個,只清晰。,最大長度和jQuery

<div contenteditable="true" placeholder="Текст сообщения" class="editable ng-valid ng-valid-maxlength ng-dirty ng-valid-parse ng-touched"></div> 
+0

您好,我編輯我的回答所以它會用jQuery工作2.1 + –

回答

0

這應該爲你工作的小提琴是here

$('#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'; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div id="editcontent" contenteditable="true"></div> 
 
<span></span>

+0

我不使用textarea的,我用contenteditable屬性div – malcoauri