2014-04-17 68 views
1

由於某些原因,\ n在javascript中不適用於我。有無論如何解決它或使用HTML?在我的情況下,我認爲使用<br>將無法​​在我的href中工作。你有什麼建議。 n不能在JavaScript中工作

Working 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/[email protected] \n" + 
     "\n Enjoy") 
}); 
+3

'\ n'確實可以在JavaScript中工作。但你的問題是,HREF得到多線,你正確的想要逃避新線,所以郵件客戶端得到'\ N',可以把它當作'\ n':TLDR:使用'\\ n',但支持可以因客戶而異。 – andlrc

+1

這可能會回答你的問題http://stackoverflow.com/questions/1155678/javascript-string-newline-character –

+0

@NULL「\\ n」似乎沒有工作......你的意思是替換「\ n 「與」\\ n「? –

回答

6

您必須對您的新行進行網址編碼。編碼的新線應%0Ahttp://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> 

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

注意,你不必硬編碼URL編碼版本,可以使用

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

PS最好不要依靠用戶的郵件客戶端進行配置嗎?使用您自己的服務器發送電子郵件,以確保所有用戶都可以發送消息。

+0

這是正確的方法。我可以看到春假已經準備好讓我放慢腳步:) – andlrc

+0

I還有另一個小問題...如果我想把網址放在超鏈接中,我該怎麼做?出於某種原因,鏈接不工作... –

+0

@LamamShalon請創建一個單獨的問題。在SO,我們喜歡關於單一事物的問題。這樣,這個問題對其他人更有用。隨時在這裏鏈接並標記我,這樣我就會收到通知。我想我知道答案,但是當你創建一個顯示你已經嘗試過的新問題時,它會容易得多。 –

1

嘗試

<br> 

,而不是

\n 

\ n在html中呈現爲1空格,所以是\ t或\ s

+0

這不適用於基於文本的電子郵件客戶端 –

+0

它適用於%0D而不是\ n –

+0

實際上,這根本不起作用,因爲[mailto的規範](http://www.ietf.org/rfc/ rfc2368.txt)表示它僅用於基於文本的電子郵件,因此,
將按原樣呈現,而不是換行符。 '「body」hname應該包含消息的第一個文本/純文本部分的內容.' –