簡體   English   中英

無法使用.attr()設置href

[英]Can't set href with .attr()

編輯:這是與擴展程序Adblock Plus相關的僅限Firefox的問題。 重新安裝插件結束了這種奇怪的行為,其中具有一些特定特殊字符的URL將使錨點完全消失。

如何使用jQuery將具有特殊字符的URL歸因於href?

我現在正在做的是:

var x = encodeURI(myURLhere)

我知道它生成有效的鏈接,因為我一直在使用console.log(x)來檢查它。

但當我這樣做時:

$("#tweet").attr("href", x);

我的錨只是消失了。

發生這種情況的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

有沒有人有任何建議,我可以做什么來將這些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); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <a href="" id="tweet">Tweet</a> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM