如何调整文本区域的大小,使其适应内容宽度,并设置最小宽度和最大宽度?
P粉277824378
P粉277824378 2023-09-05 11:21:30
[CSS3讨论组]

所以,我现在在文本区域方面遇到了麻烦,我无法将其调整为内容宽度...... 我找到了关于内容高度的长而详细的答案,并且它有效!但上面没有内容宽度...

这就是那篇文章:创建具有自动调整大小的文本区域

但是,我的问题没有解决,我也需要调整宽度。考虑最大和最小宽度。

这是示例代码:

$("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>

另外,这就是我希望它们成为的样子:

P粉277824378
P粉277824378

全部回复(1)
P粉322918729

类似这样的吗?

注意:如评论中所述,此技术使用 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;
}
<div contenteditable id="myInput"></div>
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号