简体   繁体   English

如何使textarea的高度相对于占位符内容自动调整?

[英]How to make textarea's height auto adjustable with respect to the placeholder content?

I have textarea field with long placeholder. 我有长占位符的textarea字段。 It looks good in big screens. 在大屏幕上看起来不错。 But when I open in small screens some part of placeholder becomes hidden and I have to scroll down. 但是,当我在小屏幕上打开时,占位符的某些部分会隐藏起来,因此必须向下滚动。

I want placeholder to adjust its height automatically without any scroll. 我希望占位符自动调整其高度,而无需任何滚动。

I tried a lot but I could not figure it out. 我尝试了很多,但我不知道。

Is this really possible to adjust textarea height according to the amount of text in placeholder? 这真的有可能根据占位符中的文本量来调整textarea高度吗?

Example is in this jsfiddle and snippet: 这个jsfiddle和片段中的示例:

 textarea { width: 100%; padding-bottom: 10px; padding-top: 10px; } #one { width: 100px; height: 100px; border: 1px solid; padding: 50px; } #two { width: 400px; height: 100px; border: 1px solid; padding: 50px; } 
 <div id="one"> <textarea placeholder="Sample Text Sample Text Sample Text Sample Text"></textarea> </div> <div id="two"> <textarea placeholder="Sample Text Sample Text Sample Text Sample Text "></textarea> </div> 

I've tried doing that with the textarea element and gave up. 我已经尝试过使用textarea元素做到这一点,并放弃了。 It's possible to do the same with contentEditable on a div: http://www.w3.org/TR/html5/editing.html#contenteditable 可以对div上的contentEditable进行相同的操作: http : //www.w3.org/TR/html5/editing.html#contenteditable

<div contenteditable="true"></div>

With height auto the div will grow with the content. 使用height auto时,div将随着内容的增长而增加。 You just need to catch certain keys presses with JavaScript to create the UX you want (eg enter key will on default create another element inside the editable). 您只需要使用JavaScript捕获某些按键,即可创建所需的UX(例如,Enter键将默认在可编辑内容内创建另一个元素)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM