\n 不能在 Javascript 中工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23141710/
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
\n not working in Javascript
提问by Liam Shalon
For some reason \n isn't working for me in javascript. Is there anyway to fix it or use html? In my case, I think that using <br>
will not work in my href. What are your suggestions.
出于某种原因, \n 在 javascript 中对我不起作用。有没有办法修复它或使用html?就我而言,我认为使用<br>
在我的 href 中不起作用。你有什么建议。
Working JSFiddle here.
在这里工作 JSFiddle 。
HTML:
HTML:
<a id = 'emailoff' href = "" target = "_blank">
<div id= "btn1">
<h2 id = "enter">Send Email</h2>
</div>
</a>
Javascript:
Javascript:
$('#btn1').click(function() {
$("#emailoff").attr("href", "mailto:" +
"?subject=Your ThinOptics glasses" +
"&body=To get your new ThinOptics glasses simply click this link and pick the case and color you like best. You'll get free shipping on your order. \n"+
" WWw.Thinoptics.Com/[email protected] \n" +
"\n Enjoy")
});
回答by Juan Mendes
You have to URL encode your new lines. The encoded new line should be %0A
http://jsfiddle.net/mendesjuan/LSUAh/
您必须对新行进行 URL 编码。编码的新行应该是 %0A
http://jsfiddle.net/mendesjuan/LSUAh/
<a id = 'emailoff' href = "mailto:[email protected]?subject=hello&body=a%0Ab" target = "_blank">
<div id= "btn1">
<h2 id = "enter">Send Email</h2>
</div>
</a>
If you're doing it in JS, use the following
如果你在 JS 中这样做,请使用以下内容
$('#btn1').click(function() {
$("#emailoff").attr("href", "mailto:" +
"?subject=Your ThinOptics glasses" +
"&body=To get your new ThinOptics glasses simply click this link and pick the case and color you like best. You'll get free shipping on your order. %0A"+
" WWw.Thinoptics.Com/[email protected] %0A" +
" %0A Enjoy")
});
Note that you don't have to hardcode the URL encoded version, you can use
请注意,您不必对 URL 编码版本进行硬编码,您可以使用
encodeURIComponent("\n") // %0A
P.S. Wouldn't it be better not to rely on the user's mail client being configured? Use your own server to send the email instead to make sure all users can send a message.
PS 不依赖用户配置的邮件客户端不是更好吗?使用您自己的服务器来发送电子邮件,以确保所有用户都可以发送消息。
回答by Cute_Ninja
Try
尝试
<br>
instead of
代替
\n
\n is rendered as 1 space in html and so is \t or \s
\n 在 html 中呈现为 1 个空格,\t 或 \s 也是如此