Twitter分享按钮无法正常工作

时间:2018-04-08 22:10:30

标签: javascript jquery twitter-bootstrap twitter bootstrap-4

目前正在开发freeCodeCamp的随机代码生成器项目。我和我的" Tweet"我得到了奇怪的行为。按钮。如果班级是" twitter-share-button",按钮将正确显示,但不会在弹出框中填充报价,但是如果我将班级改为"按钮",按钮没有样式,但弹出的推文框正确填充了引号和作者。

请参阅以下代码:

HTML:

<div id="quoteBox" class="col-md-6 col-md-offset-3 text-center">
<h2>“What a curious power words have.”</h2>
<p>- Tadeusz Borowski</p>
<div class="panel panel-default">
  <div class="panel-body">
    <p id="quote"></p>
    <h4 id="author"></h4>

  </div>
  <div id="twitter">
    <a class="twitter-share-button button" id="tweet-quote" data-show-count="false">Tweet</a>
  </div>​
</div>
<button id="getQuote" class="btn btn-primary btn-lg round btn-block">New Quote</button>

JS:

$(document).ready(function() {
  // Loads quote on page load
  getQuote();


  $("#getQuote").click(getQuote);
});

function getQuote() {
$(document.body).css('background-color', 
colors[Math.floor(Math.random() * colors.length)]);
$.ajax({
 type: "POST",
 url: "https://andruxnet-random-famous-quotes.p.mashape.com/?
 cat=famous",
 responseType: "json",
 success: function(response) {
  showQuote(response);
  $('#tweet-quote').attr('href', 'https://twitter.com/intent/tweet?
  text=' + encodeURIComponent('"' + response.quote + '" - ' + 
  response.author));
 },
 error: function() {
  alert('Error retrieving quote');
 },
 beforeSend: setHeader
});

function setHeader(xhr) {
 xhr.setRequestHeader("X-Mashape-Key", 
 "pmTSpn6I45mshpuPkHokTQ01lAo1p1ugEH1jsnoOS19Gk3KQvB");
 xhr.setRequestHeader("Content-Type", "application/x-www-form-
 urlencoded");
 xhr.setRequestHeader("Accept", "application/json");
}
}

function showQuote(response) {
 console.log(response);

 $('#quote').text(response.quote);
 $('#author').text(response.author);
}

我在Twitter的开发页面上花了好几个小时没有运气。非常感谢任何帮助,谢谢。

0 个答案:

没有答案