我的页面上有一个twitter共享按钮。我使用此代码是因为我想要一个自定义图标。我的问题是,我似乎无法在自定义文本中添加标签。上面写着"自定义文本"的地方就是我输入推特的地方。
<a class="icon-twitter" rel="nofollow"
href="http://twitter.com/"
onclick="popUp=window.open(
'http://twitter.com/intent/tweet?text= custom text',
'popupwindow',
'scrollbars=yes,width=800,height=400');
popUp.focus();
return false">
<i class="visuallyhidden"><img class="social-media" src="images/twitter.png"/></i>
</a>
您需要对#
进行编码。这是因为#
将被视为url的哈希部分,而不是text
查询字符串的一部分,就像&
将被解释为两个查询字符串参数之间的分隔符,而不是值的一部分一样,除非您将其编码为%26
。如果"自定义文本"可以是任何内容,请使用encodeURIComponent
:
'http://twitter.com/intent/tweet?text=' + encodeURIComponent('#custom #text')
如果该值始终是硬编码的,则用%23
替换#
即可:
'http://twitter.com/intent/tweet?text=%23custom %23text'
这更适合共享标签:https://twitter.com/intent/tweet?hashtags=Tag1,Tag2
源