2013-03-12 73 views
0

我在我的網站(http://www.xeasycorex.net)上爲每個帖子中的Tumblr按鈕使用以下腳本。該strPostTitle.replace線逃逸任何雙引號,但我需要爲單引號做同樣的,因爲它的終止字符串當存在於文章標題,而不是顯示的按鈕,我只是不知道如何做到這一點如何在Javascript中轉義單引號?

謝謝!

<script> 
var strPostUrl = "<data:post.url/>"; 
var strPostTitle = '<data:post.title/>'; 
var strNewUrl = strPostUrl.replace("http://",""); 
var strNewTitle = strPostTitle.replace(/"/g, '"'); 
document.write("<a href='http://www.tumblr.com/share/link?url="+strNewUrl+"&amp;name="+strNewTitle+"'><img src='https://lh4.googleusercontent.com/-Vw74mICSigg/USjO29GAujI/AAAAAAAARHE/dY0nzXtwTVU/s81/tumblr-share.png'/></a>"); 
</script> 
+0

使用\」逃單引號 – kinakuta 2013-03-12 02:51:39

+0

如何爲'<數據: post.url />代入? – icktoofay 2013-03-12 02:58:42

回答

0

由於單引號在javascript中似乎是特殊字符,因此您應該使用反斜槓來傳輸它。 \' 通過以下鏈接可以找到更多關於此的鏈接:http://www.w3schools.com/js/js_obj_string.asp。 我改變了你的代碼,所以現在它不需要轉義任何東西。

<script> 
     var strPostUrl = escape('<data:post.url/>'); 
    var strPostTitle = escape('<data:post.title/>'); 
    var strNewUrl = strPostUrl.replace("http://",""); 
    document.write('<a href="http://www.tumblr.com/share/link?url='+strNewUrl+'name="'+strPostTitle+'"><img src="https://lh4.googleusercontent.com/-Vw74mICSigg/USjO29GAujI/AAAAAAAARHE/dY0nzXtwTVU/s81/tumblr-share.png"/></a>'); 
     </script> 
+0

我會如何將它實現到我的代碼中? – 2013-03-12 03:08:04

+0

我編輯了答案。 – OQJF 2013-03-12 03:17:21

+0

嗯,這似乎並沒有工作。我瞭解它試圖達到的目標,但該按鈕根本不顯示 – 2013-03-12 05:43:48