2017-05-09 14 views
0

我以下的問題:我建立一個報價發生器(freecodecamp)和需要能夠送通過Twitter野兔報價。我的方法是通過Twitter網絡意圖,文本是我通過API接收的數據。它只能使用一次。Twitter分享自定義文本 - 只能一次

HTML:

<p>Share the wisdom</p> 
     <a class="twitter-share-button" href="#" target="_blank"> 
     <span class="fa-stack fa-lg"> 
      <i class="fa fa-circle fa-stack-2x"></i> 
      <i class="fa fa-twitter fa-stack-1x fa-inverse"></i> 
     </span> 
     </a> 

JS:

<script> 
    function myFunction() { 
     var firstName = document.getElementsByName("firstname")[0].value; 
     var trumpAPI = "https://api.whatdoestrumpthink.com/api/v1/quotes/personalized?q="+firstName; 
     $.getJSON(trumpAPI, function(data, status){ 
     document.getElementById("trump").innerHTML = data.message; 
     $('a[href="#"]').attr("href","https://twitter.com/intent/tweet?text="+data.message+"&hashtags=trump, quoteoftheday"); 
     }); 

     document.getElementById("trump").classList.add("border"); 

    } 
</script> 

難道是在web意圖文本參數不是動態的?任何建議如何解決這個問題?提前致謝。

回答

0

我必須猜測一點點,因爲你沒有分享您的完整功能,我認爲你做的是這樣的:

  • 你有某種形式的按鈕,點擊它會得到另一個報價。
  • 單擊該按鈕將調用您的myFunction()

如果這是正確的,那麼問題是,你的<a>元素不再有href="#"第一次函數運行之後; href現在是意圖鏈接。因此,$('a[href="#"]')不再在第二次通話中選擇該鏈接。如果您改用$(".twitter-share-button"),它應該可以工作。

+0

感謝您的快速回答/修復,並遺憾未詳細解釋。這確實是一個快速解決方案。 [看到它在這裏工作](http://onlygreatquotes.com/) – Mario