我有一个自动增长文本区域。一旦我按下回车键,高度就会调整。我想要的是,如果我删除了文本区域多行中的一行,则减小高度。这是我当前的代码:
$('.autogrow').each(function () {
h(this);
}).on('keypress', function (e) {
if(e.which == 13){
h(this);
}
});
function h(e) {
$(e).css({'height':'auto','overflow-y':'hidden'}).height(e.scrollHeight);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<textarea name="invite_email" id="invite_email" value="" class="autogrow" placeholder="Email Address" style="resize:none;overflow:hidden;-ms-overflow-style: none;min-height: 44px;border: 1px solid #e3e3e3;height: 44px; background: #fff;border-radius: 4px; padding: 6px 12px !important;font-size: 14px !important; line-height: 2; width: 100%; margin: 0;"></textarea>
最佳答案
尝试使用行数:
var ROW_HEIGHT = 13;
function resize_(e) {
var rows = txt.value.split(/\n/).length;
txt.style.height = rows * ROW_HEIGHT + 'px';
}
var txt = document.getElementById('txt');
txt.addEventListener('paste', resize_);
txt.addEventListener('input', resize_);
txt.addEventListener('change', resize_);
<textarea id="txt">row 1
row 2</textarea>
关于javascript - 如何检测我是否已删除文本区域内的多行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52050339/