【问题标题】:How to add a line wrap feature to <input>? [duplicate]如何向 <input> 添加换行功能? [复制]
【发布时间】:2021-12-13 12:14:15
【问题描述】:

我想在写入输入时按 enter 或 ctrl+enter 换行。如何实施?我尝试了textarea,但它没有输入

【问题讨论】:

  • shift+enter 作用于textarea
  • 我相信它使用的是textarea而不是常规的input

标签: javascript css angular


【解决方案1】:

input 元素只有单行。

您可以使用textarea,它在大多数(所有?)平台上都遵循 Shift+Enter。您最初可以将其设置为只有一两行高,并在需要时让它增长,我在一些 UI 中看到了这一点。

例子:

const ta = document.getElementById("ta");
ta.addEventListener("input", () => {
    const lines = Math.max(1, ta.value.split(/\r\n|\r|\n/).length);
    ta.style.height = `${lines + 1.5}em`;
});
&lt;textarea id="ta" style="height: 2.5em"&gt;&lt;/textarea&gt;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-05-11
    • 2014-11-07
    • 2021-12-28
    • 2016-08-29
    • 2021-12-10
    • 1970-01-01
    • 2017-08-24
    • 2015-06-15
    相关资源
    最近更新 更多