Html Textfield width auto width |在放下一行之前先占用水平空间
如何使文本字段具有随用户键入而扩展的自动宽度,直到达到父级宽度,然后向下(并在高度上扩展)。Html Textfield width auto width |在放下一行之前先占用水平空间,html,css,Html,Css,如何使文本字段具有随用户键入而扩展的自动宽度,直到达到父级宽度,然后向下(并在高度上扩展)。 似乎是因为某种原因,“宽度:自动”根本不起作用。 谢谢。我认为不可能用输入元素来实现,但你可以欺骗它 用叉子叉 $(“.nice输入”).keyup(函数(){ if($(`input[nice rel=“${$(this).attr(“id”)}]`).length>0){ $(`input[nice rel=“${$(this).attr(“id”)}”]`).val($(this.text
似乎是因为某种原因,“宽度:自动”根本不起作用。
谢谢。我认为不可能用
输入元素来实现,但你可以欺骗它
用叉子叉
$(“.nice输入”).keyup(函数(){
if($(`input[nice rel=“${$(this).attr(“id”)}]`).length>0){
$(`input[nice rel=“${$(this).attr(“id”)}”]`).val($(this.text());
}
});代码>
.container{
宽度:120px;
背景色:#F5;
边框:1px实心#ededed;
边界半径:5px;
填充:20px;
}
.container.input{
字体:无衬线;
显示:内联块;
大纲:无;
宽度:自动;
最小宽度:50px;
边框底部:1px实心#000;
}
HTML:
<div class="parent">
<textarea></textarea>
</div>
jQuery:
$("textarea").keypress(function() {
// Increases the length of textarea
$("textarea").css({
'width' : $(this).width() + 10
});
// Increases the height when textarea width reaches maximum length
if ($(this).width() >= $(this).parent().width()) {
$(this).css({
'height' : $(this).height() + 20
})
}
}
请在
它几乎完全按照所描述的那样工作。如果有人找到一种方法来确定文本区域内内容的宽度,它将实现这一目标。现在,你们可以用它作为垫脚石。在什么情况下使用它,而不是将输入设置为最大宽度?只需将元素宽度设为100%,或将内容复制到表单元素。@OfficialAntarctica如果输入文本的宽度大于父元素的宽度,则无法下拉。请使用textarea
元素或使用下面的我的答案↓
$("textarea").keypress(function() {
// Increases the length of textarea
$("textarea").css({
'width' : $(this).width() + 10
});
// Increases the height when textarea width reaches maximum length
if ($(this).width() >= $(this).parent().width()) {
$(this).css({
'height' : $(this).height() + 20
})
}
}