【问题标题】:Textarea automatic line breakTextarea 自动换行
【发布时间】:2015-09-13 04:21:58
【问题描述】:

我的网站上有一个文本区域,我想在其中粘贴 URL。

是否可以在每次粘贴 URL 时创建换行符?如果没有,我输入空格时是否可以创建换行符?

我已经搜索了解决方案,但我发现的只是在表单提交后创建换行符的解决方案,这对我没有帮助。

【问题讨论】:

  • 换行是什么意思?每次在 textarea 上粘贴内容时,是否要创建 <br /> 标签?

标签: javascript html textarea space line-breaks


【解决方案1】:

在这里提琴:https://jsfiddle.net/3sj2644z/

this.value = this.value + "\n";

您在 textarea 上侦听 paste 事件,然后抓取当前在其中的文本,并在其中附加一个带有转义字符 \n 的换行符,然后将新的字符串值放回 textarea。

你没有在 textarea 中使用 html,所以如果你这样认为 br 标记不起作用。

【讨论】:

    【解决方案2】:

    function ConvertToLinks() {
      str = document.getElementById("S1").value;
      str = str.replace(/\r\n|\n/g,'<br>');
      document.getElementById('txtLinks').innerHTML = str;
    }
    <html>
    <head>
    <title>Text area redirect</title>
    
    </head>
    <body>
    <textarea rows="5" id="S1" name="S1" cols="40">
    <a href="http://yahoo.com">Yahoo</a>
    <a href="http://google.com">Google</a>
    
    <a href="http://webdeveloper.com">Web Developer</a>
    <a href="http://www.codingforums.com/javascript-programming/195565-place-links-textarea.html">from Web</a>
    </textarea>
    <br><button onclick="ConvertToLinks()">Convert to Links</button>
    <div id="txtLinks" style="width:350px;min-height:100px;border:1px solid red"></div>
    </body>
    </html>
    由http://www.codingforums.com/javascript-programming/195565-place-links-textarea.html

    【讨论】:

    • 谢谢,但这不是我想要的。例如,让我们复制google.com。我将它粘贴到我的文本区域中,粘贴后我想自动插入换行符,因此链接不会水平相邻。这样的事情可能吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-05
    • 2012-03-29
    • 2011-10-19
    • 1970-01-01
    相关资源
    最近更新 更多