0

我在我的网站 ( http://www.xeasycorex.net ) 上使用以下脚本作为每个帖子中的 Tumblr 按钮。该strPostTitle.replace行转义任何双引号,但我需要对单引号做同样的事情,因为它在帖子标题中出现并且不显示按钮时终止字符串,我只是不知道该怎么做

谢谢!

<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>
4

2 回答 2

0

使用encodeURIComponentJavaScript 函数:

var strPostUrl   = encodeUIRComponent("<data:post.url/>");
var strPostTitle = encodeURIComponent("<data:post.title/>");

下面是函数的解释:

https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/encodeURIComponent

于 2013-03-12T03:21:59.577 回答
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>
于 2013-03-12T02:54:39.147 回答