encodeuriccomponent不能与Twitter按钮一起工作

encodeURIComponent not working with Twitter button

本文关键字:一起 工作 按钮 Twitter 不能 encodeuriccomponent      更新时间:2023-09-26

我目前正试图使一个应用程序,推文出的报价,目前在文本泡沫。但是,当我使用这行代码时,

<a class="twitter-share-button" href="https://twitter.com/intent/tweet?text" + encodeURIComponent(quote.quote + ' - ' + quote.author);>Tweet</a>

它不会带我去任何地方。是否有任何方法我可以得到这个工作,或者我需要使用外部Twitter按钮?

谢谢!

CodePen Link: http://codepen.io/Jelani/full/rOmrOJ/

你不能像那样在标记中嵌入javascript。您可以采取的一种方法是向randomQuote函数添加一些代码,如下所示:

var randomQuote = function() {
  ... // current code
  var text = quote.quote + ' - ' + quote.author;
  $('.twitter-share-button').attr('href', "https://twitter.com/intent/tweet?text="+encodeURIComponent(text));
}

另一种方法是将quote变量声明移出randomQuote函数,并在twitter-share-button

上设置onclick
<a class="twitter-share-button" onclick="goTweet()">Tweet</a>

调用一个函数,该函数执行如下操作:

function goTweet() {
  var text = quote.quote + ' - ' + quote.author;
  window.location.href = "https://twitter.com/intent/tweet?text=" +  encodeURIComponent(text);
}