2016-02-27 106 views
4

我的JSON工作正常。每次點擊我的「Gimme A'Quote」按鈕時,我都可以修改報價文本和報價作者。但是,每次嘗試使用報價單的文字和作者發佈推文時,都會遇到問題。每當我點擊twitter符號時,頁面都會刷新。不能分享鳴叫包含報價和作者(FCC隨機報價機)JavaScript/JQuery

我一直在試圖這樣做是爲了增加報價的內容和作者到鳴叫:

var twitterIntent = 'https://twitter.com/intent/tweet?text=' + quoteText + 'Author' + quoteAuthor + ' @myleschuahiock'; 

而且隨後告訴jQuery來做到這一點,每當我點擊「給我一個」報價」按鈕:

$('.quote-button').click(function(){ 
    getQuote(); 
    colorRandomizer(); 
    $('.twitter-share').attr("href", twitterIntent); 
}); 

我的代碼位於: http://codepen.io/myleschuahiock/pen/WrVgzB 請看看!

有人能告訴我我做錯了什麼嗎? 非常感謝!

這個項目是我提交給Free Code Camp的。

回答

2

您正在定義getQuote()函數中的twitterIntent,但隨後嘗試在該函數之外使用它。如果您檢查控制檯是否有錯誤,則表明twitterIntent在您點擊按鈕後未定義。相反,您只需更改getQuote()函數內的.twitter-share鏈接的href屬性即可。

只需添加行$('.twitter-share').attr('href','https://twitter.com/intent/tweet?text='+quoteText+' Author: '+quoteAuthor+' @myleschuahiock');,您可以在此更改.quote-content.quote-author區域的內容,如下例所示。

json.forEach(function(val) { 
    quoteText = val.quote; 
    quoteAuthor = val.author; 
    $('.quote-content').html(quoteText); 
    $('.quote-author').html(quoteAuthor); 
    $('.twitter-share').attr('href','https://twitter.com/intent/tweet?text='+quoteText+' Author: '+quoteAuthor+' @myleschuahiock'); 
    }); 

然後,所有你需要做的就是從$('.quote-button').click()刪除$('.twitter-share').attr("href", twitterIntent);,和你所有的設置。

+0

太棒了!感謝您簡化我的代碼! @ShamSUP! – myleschuahiock