所以,我现在在文本区域方面遇到了麻烦,我无法将其调整为内容宽度...... 我找到了关于内容高度的长而详细的答案,并且它有效!但上面没有内容宽度...
这就是那篇文章:创建具有自动调整大小的文本区域
但是,我的问题没有解决,我也需要调整宽度。考虑最大和最小宽度。
这是示例代码:
$("textarea").each(function () {
this.setAttribute("style", "height:" + (this.scrollHeight) + "px;");
}).on("input", function () {
this.style.height = 0;
this.style.height = (this.scrollHeight) + "px";
});
$("textarea").trigger("input");
textarea{
border: 1px solid black;
min-width: 50px;
max-width: 300px;
width: auto;
}
<script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.6.3.min.js"></script> <textarea>this is my normal content, and it has to consider this content to be as long as the max-width</textarea> <textarea>this is my normal content, should be consider</textarea> <textarea>this is my min content</textarea>
另外,这就是我希望它们成为的样子:
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
类似这样的吗?
注意:如评论中所述,此技术使用
contenteditable属性#myInput { min-height: 20px; max-height: 100px; overflow: auto; border: 1px solid #000; border-radius: 4px; max-width: 80%; min-width: 40px; display: inline-block; padding: 7px; resize: vertical; }