js代码
/**
* 类样式为form-textarea的textarea自动扩展高度
*/
(function($) {
function textareaAutoHeight(e) {
if(e.keyCode == 8 || e.keyCode == 46) {
var text = $(this).val();
$(this).removeAttr('style');
}
var scrollH = this.scrollHeight,
height = $(this).outerHeight() - 1;
if(scrollH > height) {
$(this).height(scrollH - 4);
}
}
$(document).on('keyup', '.form-textarea', textareaAutoHeight);
$(document).on('keydown', '.form-textarea', textareaAutoHeight);
$(function() {
$(".form-textarea").each(textareaAutoHeight);
});
})(jQuery);
html引用
<textarea class="form-control form-textarea" data-rule-required="true" aria-required="true"></textarea>
只需要在textarea的class中添加上form-textarea即可
共同交流联系我:vaeyang1001@gmail.com