我的网站上有一个textarea,我想粘贴网址。
每次粘贴网址时都可以创建换行符吗?如果没有,当我输入空格时它可以创建换行符吗?
我已经搜索了解决方案,但我找到的只是在表单提交后创建换行符的解决方案,这对我没有帮助。
答案 0 :(得分:2)
Fiddle here: https://jsfiddle.net/3sj2644z/
this.value = this.value + "\n";
You listen for the paste event on the textarea and you grab the text that is currently in it and append a linebreak with the escape character \n to it and then place that new string value back into the textarea.
You don't use html in the textarea so the br tag doesn't work if you think so.
答案 1 :(得分:0)
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>