简体   繁体   English

在 input type="text" 元素 HTML/CSS 中包装文本

[英]Wrapping text inside input type="text" element HTML/CSS

The HTML shown below,下图的HTML,

<input type="text"/>

is displayed in a browser like so:在浏览器中显示如下:


When I add the following text, 当我添加以下文本时,

The quick brown fox jumped over the lazy dog.敏捷的棕色狐狸跳过了懒狗。

Using the HTML below,使用下面的 HTML,

 <input type="text" value="The quick brown fox jumped over the lazy dog."/>

it is displayed in a browser like so:它在浏览器中显示如下:


But I would like it to be displayed in a browser like so: 但我希望它像这样显示在浏览器中:


I want the text in my input element to wrap. 我希望输入元素中的文本换行。 Can this be accomplished without a textarea? 这可以在没有文本区域的情况下完成吗?

That is the textarea 's job - for multiline text input.那是textarea的工作 - 用于多行文本输入。 The input won't do it ; input不会这样做 it wasn't designed to do it.它不是为这样做而设计的。

So use a textarea .所以使用textarea Besides their visual differences, they are accessed via JavaScript the same way (use value property).除了视觉上的差异之外,它们还可以通过 JavaScript 以相同的方式访问(使用value属性)。

You can prevent newlines being entered via the input event and simply using a replace(/\\n/g, '') .您可以防止通过input事件input换行符,只需使用replace(/\\n/g, '')

Word Break will mimic some of the intent Word Break 会模仿一些意图

 input[type=text] { word-wrap: break-word; word-break: break-all; height: 80px; }
 <input type="text" value="The quick brown fox jumped over the lazy dog" />

As a workaround, this solution lost its effectiveness on some browsers.作为一种变通方法,此解决方案在某些浏览器上失去了有效性。 Please check the demo: http://cssdesk.com/dbCSQ请查看演示: http : //cssdesk.com/dbCSQ

You can not use input for it, you need to use textarea instead.不能为它使用输入,您需要使用 textarea 代替。 Use textarea with the wrap="soft" code and optional the rest of the attributes like this:将 textarea 与wrap="soft"代码和可选的其余属性一起使用,如下所示:

<textarea name="text" rows="14" cols="10" wrap="soft"> </textarea>

Atributes: To limit the amount of text in it for example to "40" characters you can add the attribute maxlength="40" like this: <textarea name="text" rows="14" cols="10" wrap="soft" maxlength="40"></textarea> To hide the scroll the style for it.属性:要将其中的文本数量限制为例如“40”个字符,您可以像这样添加属性maxlength="40"<textarea name="text" rows="14" cols="10" wrap="soft" maxlength="40"></textarea>隐藏滚动样式。 if you only use overflow:scroll;如果你只使用overflow:scroll; or overflow:hidden;overflow:hidden; or overflow:auto;overflow:auto; it will only take affect for one scroll bar.它只会对一个滚动条有效。 If you want different attributes for each scroll bar then use the attributes like this overflow:scroll; overflow-x:auto; overflow-y:hidden;如果你想为每个滚动条设置不同的属性,那么使用像这样的属性overflow:scroll; overflow-x:auto; overflow-y:hidden; overflow:scroll; overflow-x:auto; overflow-y:hidden; in the style area: To make the textarea not resizable you can use the style with resize:none;在样式区域中:要使 textarea 不可调整大小,您可以使用带有resize:none;的样式resize:none; like this:像这样:

<textarea name="text" rows="14" cols="10" wrap="soft" maxlength="40" style="overflow:hidden; resize:none;></textarea>

That way you can have or example a textarea with 14 rows and 10 cols with word wrap and max character length of "40" characters that works exactly like a input text box does but with rows instead and without using input text.这样,您可以拥有或示例一个包含 14 行和 10 列的文本区域,带有自动换行和“40”个字符的最大字符长度,其工作方式与输入文本框完全相同,但使用行而不使用输入文本。

NOTE: textarea works with rows unlike like input <input type="text" name="tbox" size="10"></input> that is made to not work with rows at all.注意: textarea 与行不同,就像 input <input type="text" name="tbox" size="10"></input> ,它根本不与行一起工作。

To create a text input in which the value under the hood is a single line string but is presented to the user in a word-wrapped format you can use the contenteditable attribute on a <div> or other element:要创建一个文本输入,其中引擎盖下的值是单行字符串但以自动换行格式呈现给用户,您可以在<div>或其他元素上使用contenteditable属性:

 const el = document.querySelector('div[contenteditable]'); // Get value from element on input events el.addEventListener('input', () => console.log(el.textContent)); // Set some value el.textContent = 'Lorem ipsum curae magna venenatis mattis, purus luctus cubilia quisque in et, leo enim aliquam consequat.'
 div[contenteditable] { border: 1px solid black; width: 200px; }
 <div contenteditable></div>

I know this is old, but no one mentioned this so I thought I would add a reason for wanting this.我知道这是旧的,但没有人提到这一点,所以我想我会添加一个想要这个的理由。

If one has a need for storing data into a hidden form field (like a large string), that field can easily be displayed for testing by changing 'hidden' to 'text' and back to 'hidden' when done testing.如果需要将数据存储到隐藏的表单字段(如大字符串)中,则可以通过在完成测试时将“隐藏”更改为“文本”并返回“隐藏”来轻松显示该字段以进行测试。 A textarea cannot.文本区域不能。 It is a completely different object.它是完全不同的 object。

With that said, a textarea can have the css display property set to 'none' to hide it and still allow content to be accessed.话虽如此,文本区域可以将 css 显示属性设置为“无”以隐藏它并仍然允许访问内容。

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

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