javascript 如何让 textarea 填充其父宽度的宽度?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3378064/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 01:00:53  来源:igfitidea点击:

How to make textarea fill the width of its parent width?

javascripthtmlcssxhtml

提问by johnjohn

Given that we don't know the div's width beforehand?

鉴于我们事先不知道 div 的宽度?

My idea: Calculate the div 's width with JS, find a way to convert it to cols and the apply the css to the textarea onthe fly. But perhaps there 's no need to reinvent the wheel?

我的想法:使用 JS 计算 div 的宽度,找到一种方法将其转换为 cols 并将 css 即时应用于 textarea。但也许没有必要重新发明轮子?

Thank you.

谢谢你。

回答by Krevan

<textarea style="width:100%">* will work in most cases.

<textarea style="width:100%">* 将在大多数情况下工作。

If it doesn't, please provide more information!

如果没有,请提供更多信息!

* Note: for some reason, it will need the colsand rowsattributes to validate. But setting a width or a height will override them, so put any value you want.

* 注意:由于某种原因,它需要colsrows属性来验证。但是设置宽度或高度会覆盖它们,所以输入任何你想要的值。