1

大家早上好,我知道以前有人问过这个问题,但是作为我正在努力学习的 Javascript 新手,我无法理解如何让我的 Twitter 按钮分享随机引用。如果可能的话,请一位开发人员向我解释如何实际输入它。我对 HTML、CSS 和 Bootstrap 有很好的理解,但希望有人向我解释这一点。

提前谢谢了。

var quotes = [
  "\'It is never too late to be what you might have been.\' - George Eliot",
  "\'What lies behind us and what lies before us are tiny matters compared to what lies within us.\' - Henry Stanley Haskins",
  "\'Great thoughts speak only to the thoughtful mind, but great actions speak to all mankind.\' - Emily P. Bissell",
  "\'I haven’t failed. I’ve just found 10,000 ways that don’t work.\' - Thomas Edison",
  "\'In between goals is a thing called life, that has to be lived and enjoyed.\' - Sid Caesar"
]
function newQuote() {
  var randomNumber = Math.floor(Math.random() * (quotes.length));
  document.getElementById('quoteDisplay').innerHTML = quotes[randomNumber];
}
<html>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" integrity="sha384-/Y6pD6FV/Vv2HJnA6t+vslU6fwYXjCFtcEpHbNJ0lyAFsXTsjBbfaDjzALeQsN6M" crossorigin="anonymous">

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<body>
  
  
  <h1>Simple Quote Generator</h1>
  <div id="quoteDisplay">
  </div>
    
  <div class="quoteButton">
  <button onclick="newQuote()">New Quote</button>
   
  </div>
  <a href="http://twitter.com/intent/tweet/?text" title="Share on Twitter" target="blank" class="btn btn-twitter-"><i class="fa fa-twitter"></i> Twitter</a>
  <script src="javascript.js"></script>
</body>
</html>

4

2 回答 2

0

您的链接目标是静态的,它始终是twitter.com/intent/tweet/?text. 我不熟悉 Twitter API,但我猜它希望?text=some&encoded&text在 URL 的末尾有类似的东西。您需要设置文本的值:

<a href="" id="twitterLink">

function newQuote() {
  var randomNumber = Math.floor(Math.random() * (quotes.length));
  document.getElementById('quoteDisplay').innerHTML = quotes[randomNumber];

  document.getElementById('twitterLink').href = "http://twitter.com/intent/tweet/?text=" + encodeURIComponent(quotes[randomNumber])

}
于 2017-09-06T10:23:03.440 回答
0
<span class="tweetquote" style="margin:15px; padding:15px; border:1px solid #ddd;">
<a  href="https://twitter.com/intent/tweet?url=https://twitter.com&;text=Tweet This piece of content the Entire quote in content and its url&;via=twitter&;" title="Tweet This piece of content the Entire quote in content and its url" target="_blank" rel="noopener noreferrer">Tweet This piece of content the Entire quote in content and its url</a>
<a href="https://twitter.com/intent/tweet?url=https://twitter.com&;text=Tweet This piece of content the Entire quote in content and its url&;via=twitter&;" title="Tweet This piece of content the Entire quote in content and its url" target="blank" class="btn btn-twitter-"><i class="fa fa-twitter"></i>Tweet</a>

于 2019-01-08T11:19:48.417 回答