\n不能在Javascript中工作

Lia*_*lon 1 html javascript jquery

出于某种原因\n在javascript中不适合我.无论如何要修复它或使用HTML?在我的情况下,我认为使用<br>将无法在我的href中工作.你有什么建议.

在这里工作JSFiddle .

HTML:

<a id = 'emailoff' href = "" target = "_blank">
    <div id= "btn1">
        <h2 id = "enter">Send Email</h2>
    </div>
</a>
Run Code Online (Sandbox Code Playgroud)

使用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/teddy@shalon.com \n" +
        "\n Enjoy")
});
Run Code Online (Sandbox Code Playgroud)

Jua*_*des 7

您必须对新行进行URL编码.编码的新行应该是 %0A http://jsfiddle.net/mendesjuan/LSUAh/

<a id = 'emailoff' href = "mailto:me@you.com?subject=hello&body=a%0Ab" target = "_blank">
    <div id= "btn1">
        <h2 id = "enter">Send Email</h2>
    </div>
</a>
Run Code Online (Sandbox Code Playgroud)

如果您在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/teddy@shalon.com  %0A" +
        " %0A Enjoy")
});
Run Code Online (Sandbox Code Playgroud)

请注意,您不必对URL编码版本进行硬编码,您可以使用

encodeURIComponent("\n") // %0A
Run Code Online (Sandbox Code Playgroud)

PS不依靠用户的邮件客户端配置不是更好吗?使用您自己的服务器发送电子邮件,以确保所有用户都可以发送消息.