\ n不能在Javascript中工作

时间:2014-04-17 19:22:27

标签: javascript jquery html

出于某种原因\ n在javascript中没有为我工作。无论如何要修复它或使用HTML?就我而言,我认为使用<br>在我的href中不起作用。你有什么建议。

使用JSFiddle here

HTML:

<a id = 'emailoff' href = "" target = "_blank">
    <div id= "btn1">
        <h2 id = "enter">Send Email</h2>
    </div>
</a>

使用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")
});

2 个答案:

答案 0 :(得分: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>

如果您在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")
});

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

encodeURIComponent("\n") // %0A

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

答案 1 :(得分:1)

尝试

<br>

而不是

 \n

\ n在html中呈现为1个空格,因此是\ t或\ s