文本区域自动换行

Textarea automatic line break

本文关键字:自动换行 区域 文本      更新时间:2023-09-26

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

这是否可以在每次粘贴URL时创建换行符?如果没有,当我进入空间时,它能创建换行符吗?

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

点击此处:https://jsfiddle.net/3sj2644z/

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

您在文本区域上侦听粘贴事件,然后获取当前在其中的文本,并在其中附加一个带转义符的换行符,然后将新字符串值放回文本区域。

您不在文本区域中使用html,因此br标记不起作用。

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