Tel*_*per 3 html javascript jquery
编辑:这是与扩展程序Adblock Plus相关的仅限Firefox的问题.重新安装插件结束了这种奇怪的行为,其中具有一些特定特殊字符的URL会使锚点完全消失.
如何使用jQuery将具有特殊字符的URL归因于href?
我现在正在做的是:
var x = encodeURI(myURLhere)
Run Code Online (Sandbox Code Playgroud)
我知道它生成有效的链接,因为我一直在使用console.log(x)它来检查它.
但当我这样做时:
$("#tweet").attr("href", x);
Run Code Online (Sandbox Code Playgroud)
我的锚只是消失了.
发生这种情况的URL的一个示例:
https://twitter.com/intent/tweet?text=%22If%20it%20is%20not%20right%20do%20not%20do%20it;%20if%20it%20is%20not%20true%20do%20not%20say%20it.%22%20%E2%80%93%20Marcus%20Aurelius
Run Code Online (Sandbox Code Playgroud)
有没有人有任何建议,我可以做什么来将这些URL归因于我的主播的href?
问题是因为;字符串中的字符在使用时未编码encodeURI,而是需要将查询字符串拆分并直接调用encodeURIComponent()它.
注意:要使此代码段生效,您需要在新标签页中打开"推文"链接,因为无法在iframe中显示Twitter.
var text = 'If it is not right do not do it; if it is not true do not say it." – Marcus Aurelius"'
var x = encodeURIComponent(text);
$("#tweet").attr("href", 'https://twitter.com/intent/tweet?text=' + x);Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href="" id="tweet">Tweet</a>Run Code Online (Sandbox Code Playgroud)