javascript 将 <br/> 标记附加到 textarea jquery 中的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10926541/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Append a <br/> tag to a value inside a textarea jquery
提问by tomexsans
How would you append a
tag on a value inside a textarea, it only appends it after a keypress of SHIFT+ENTER,
您将如何
在 textarea 内的值上附加标签,它只会在按下 SHIFT+ENTER 后附加它,
here is my code to append on the text area and it does not work??
这是我要附加到文本区域的代码,但它不起作用?
$('#textarea').append("<br/>");
i think there are still something lacking.
$('#textarea').append("<br/>");
我认为还缺少一些东西。
Thank you. . .
谢谢你。. .
回答by Sampson
Are you actually wanting to place the characters in the value?
你真的想把字符放在值中吗?
?$("textarea").val(function(i,v){
return v + "<br/>";
});????
Or simply add a new line?
或者只是添加一个新行?
?$("textarea").val(function(i,v){
return v + "\nfoo";
});???????
Fiddle: http://jsfiddle.net/jonathansampson/SNeyy/
小提琴:http: //jsfiddle.net/jonathansampson/Sneyy/
If you want to respond only to shift+ enter:
如果您只想回复shift+ enter:
$("textarea").on("keypress", function(e){
if ( e.which === 13 && e.shiftKey ) {
$(this).val(function(i,v){
return v + "<br/>"; // or return v + "\n"; (whatever you want)
});
}
});????
回答by Joyce Babu
You are trying to change the value of the textarea
. To add the string "<br/>" you have to use
您正在尝试更改textarea
. 要添加字符串“<br/>”,您必须使用
$('#textarea').val($('#textarea').val() + '<br/>');
and to add a new line you have to use
并添加一个新行,你必须使用
$('#textarea').val($('#textarea').val() + "\n");
Textarea can have only a text node as its child. <textarea><br/></textarea>
is incorrect usage. If you want to add the string "<br/>", the html code should be
Textarea 只能有一个文本节点作为其子节点。<textarea><br/></textarea>
是不正确的用法。如果要添加字符串“<br/>”,html代码应该是
<textarea><br/></textarea>
and to add a new line, the html code should be
并添加一个新行,html代码应该是
<textarea>
</textarea>
In other words, you cannotuse <textarea><br/></textarea>
换句话说,您不能使用<textarea><br/></textarea>