显示/隐藏 textarea 改变它的高度
show / hide textarea changes its height
点击ŞHOW / HIDE
几次。
你会看到每次点击后textarea会越来越高
这是什么原因以及如何避免这种情况。
我需要文本区域始终适合内容。
$('#btna').on('click', function(){
$('#txa').hide()
});
$('#btnb').on('click', function(){
$('#txa').show()
let a = $('#txa').prop('scrollHeight');
$('#txa').height(a);
});
#txa, #txb{
display:block;
width:100%;
resize:none;
overflow:hidde;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script>
<textarea id='txa'>
lorem ipsum
lorem ipsum
</textarea>
<button id='btnb'>SHOW</button>
<button id='btna'>HIDE</button>
给你,高度操作是不必要的。
$('#btna').on('click', function(){
$('#txa').hide()
});
$('#btnb').on('click', function(){
$('#txa').show()
});
#txa, #txb{
display:block;
width:100%;
resize:none;
overflow:hidde;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script>
<textarea id='txa'>
lorem ipsum
lorem ipsum
</textarea>
<button id='btnb'>SHOW</button>
<button id='btna'>HIDE</button>
请试试这个。
$('#btna').on('click', function(){
$('#txa').hide()
});
$('#btnb').on('click', function(){
$('#txa').show()
});
autosize();
function autosize(){
var text = $('#txa');
text.each(function(){
$(this).attr('rows',1);
resize($(this));
});
text.on('input', function(){
resize($(this));
});
function resize ($text) {
$text.css('height', 'auto');
$text.css('height', $text[0].scrollHeight+'px');
}
}
#txa, #txb{
display:block;
width:100%;
resize:none;
overflow:hidden;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script>
<textarea id='txa' name="md-content">
lorem ipsum
lorem ipsum
sdf
sdf
sdf
sdf
sdf
sdf
s
</textarea>
<button id='btnb'>SHOW</button>
<button id='btna'>HIDE</button>
从您的脚本中删除以下两行
let a = $('#txa').prop('scrollHeight');
$('#txa').height(a);
它仍然有效,并且不会在 textarea 字段中添加额外的行。
点击ŞHOW / HIDE
几次。
你会看到每次点击后textarea会越来越高
这是什么原因以及如何避免这种情况。
我需要文本区域始终适合内容。
$('#btna').on('click', function(){
$('#txa').hide()
});
$('#btnb').on('click', function(){
$('#txa').show()
let a = $('#txa').prop('scrollHeight');
$('#txa').height(a);
});
#txa, #txb{
display:block;
width:100%;
resize:none;
overflow:hidde;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script>
<textarea id='txa'>
lorem ipsum
lorem ipsum
</textarea>
<button id='btnb'>SHOW</button>
<button id='btna'>HIDE</button>
给你,高度操作是不必要的。
$('#btna').on('click', function(){
$('#txa').hide()
});
$('#btnb').on('click', function(){
$('#txa').show()
});
#txa, #txb{
display:block;
width:100%;
resize:none;
overflow:hidde;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script>
<textarea id='txa'>
lorem ipsum
lorem ipsum
</textarea>
<button id='btnb'>SHOW</button>
<button id='btna'>HIDE</button>
请试试这个。
$('#btna').on('click', function(){
$('#txa').hide()
});
$('#btnb').on('click', function(){
$('#txa').show()
});
autosize();
function autosize(){
var text = $('#txa');
text.each(function(){
$(this).attr('rows',1);
resize($(this));
});
text.on('input', function(){
resize($(this));
});
function resize ($text) {
$text.css('height', 'auto');
$text.css('height', $text[0].scrollHeight+'px');
}
}
#txa, #txb{
display:block;
width:100%;
resize:none;
overflow:hidden;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script>
<textarea id='txa' name="md-content">
lorem ipsum
lorem ipsum
sdf
sdf
sdf
sdf
sdf
sdf
s
</textarea>
<button id='btnb'>SHOW</button>
<button id='btna'>HIDE</button>
从您的脚本中删除以下两行
let a = $('#txa').prop('scrollHeight');
$('#txa').height(a);
它仍然有效,并且不会在 textarea 字段中添加额外的行。