如何调整文本区域的大小,使其适应内容宽度,并设置最小宽度和最大宽度?
P粉277824378
P粉277824378 2023-09-05 11:21:30
0
1
522
<p>所以,我现在在文本区域方面遇到了麻烦,我无法将其调整为内容宽度...... 我找到了关于内容高度的长而详细的答案,并且它有效!但上面没有内容宽度...</p> <p>这就是那篇文章:创建具有自动调整大小的文本区域</p> <p>但是,我的问题没有解决,我也需要调整宽度。考虑最大和最小宽度。</p> <p>这是示例代码:</p> <p> <pre class="brush:js;toolbar:false;">$("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");</pre> <pre class="brush:css;toolbar:false;">textarea{ border: 1px solid black; min-width: 50px; max-width: 300px; width: auto; }</pre> <pre class="brush:html;toolbar:false;"><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></pre> </p> <p>另外,这就是我希望它们成为的样子:</p>
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>
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!