简体   繁体   English

如何扩展textarea以适合垂直和水平放置文本?

[英]How to expand textarea to fit text vertically and horizontally?

How can I do something like this using html, css and jquery? 我如何使用html,css和jquery做类似的事情?

I found this question Textarea to resize based on content length but it only expands height of textarea. 我发现这个问题Textarea可以根据内容长度来调整大小,但是只会扩大textarea的高度。

I want that my textarea precisely imitate what user is inputing. 我希望我的文本区域精确地模仿用户输入的内容。 So if user is writing text into a row, row doesnt end until the user hit "enter". 因此,如果用户将文本写入一行,则直到用户单击“ enter”(输入),行才结束。 The width of textarea could be larger than the window. 文本区域的宽度可以大于窗口的宽度。 Same thing for the height. 高度相同。

Also Im searching for solution which allows me to change the fontsize and the textarea resize to fit it. 我也在寻找解决方案,使我可以更改字体大小和调整textarea的大小以适应它。

Does anyone have an idea? 有人有主意吗?

the solution for the height is fine. 高度的解决方案很好。

to make it look horizontally proper, add the following css, 为了使其在水平方向上看起来合适,请添加以下CSS,

 textarea { width: 100%; display: block; resize: vertical; } 

Use onkeypress even 甚至使用onkeypress

see this example : http://jsfiddle.net/kevalbhatt18/bkyxz8cc/3/ 参见以下示例: http : //jsfiddle.net/kevalbhatt18/bkyxz8cc/3/

 <textarea id="txt" placeholder="name" class="form" type="text" onkeypress="this.style.width = ((this.value.length + 1) * 8) + 'px';"></textarea> 

And for placeholder on load use jquery and apply placeholder size in to input 对于占位符 ,请使用jquery并将占位符大小应用到输入中

 $('textarea').css('width',((input.getAttribute('placeholder').length + 1) * 8) + 'px'); 

Even you can use id instead of input this is just an example so that I used $(input) 即使您可以使用id代替输入,这也只是一个示例,因此我使用了$(input)

And in css provide min-width 并在CSS中提供最小宽度

 .form { border: 1px solid black; text-align: center; outline: none; min-width:4px; } 


If you remove all text from textarea box then it will take placeholder value using focusout 如果您从textarea框中删除所有文本,则它将使用focusout获取占位符值

  $("textarea").focusout(function(){ if(this.value.length>0){ this.style.width = ((this.value.length + 1) * 8) + 'px'; }else{ this.style.width = ((this.getAttribute('placeholder').length + 1) * 8) + 'px'; } }); 

For change the fontsize and the textarea resize 用于更改字体大小和文本区域的大小

use button click 使用按钮点击

 $('button').on('click',function(){ var style={ "font-size":"20px", "height":"90px", "width":"40%", } $('textarea').css(style); }) 

Thanks everyone for their advices and responses, but finally I came with my own, a little bit workaround, solution. 感谢每个人的建议和回应,但最后我提出了自己的解决方案。

So I used this Textarea to resize based on content length as script expanding the height of textarea. 因此,当脚本扩展了textarea的高度时,我使用了Textarea来根据内容长度调整大小

To expand the width of textarea I created a hidden div, whom I copied the text from textarea and recursively I assigned the width of div to textarea. 为了扩大textarea的宽度,我创建了一个隐藏的div,我从textarea复制了文本,然后递归地将div的宽度分配给textarea。 Of course you have to set same styling of text for both elements. 当然,您必须为两个元素设置相同的文本样式。

Also as I wanted the textarea to be able to inflate itself "to infinite and beyond", thats why Im changing the width of html and body also. 另外,由于我希望textarea能够将自身“膨胀到无限甚至超越”,这就是为什么Im还要更改html和body的宽度。

function textAreaAdjust(o) {
    o.style.height = "1px";
    o.style.height = (90+o.scrollHeight)+"px";
    x = $('textarea').val().replace(/&/g, '&amp;')
     .replace(/>/g, '&gt;')
     .replace(/</g, '&lt;')
     .replace(/ /g, '&nbsp;')
     .replace(/\n/g, '<br>');
    $('div').html(x);
    $('html').css('width',$('div').width()+50);
    $('body').css('width',$('html').width());
    $('textarea').css('width',$('html').width());
}

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

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