2013-03-12 95 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