[英]Changing the value of data-text attribute of the Twitter button dynamically based on input data from an API using jquery
我設計了一個頁面,單擊“獲取報價”按鈕將顯示div中的各種報價,這些報價是從API獲取的。 我想添加一個Twitter shate按鈕。 但是,我對如何更改Twitter按鈕的“數據文本”屬性的值感到困惑,因為它動態地更改了API引號的值。 我使用jQuery處理來自API的數據,那么如何使用jQuery更改“數據文本”的值?
代碼是:
HTML:
<body>
<h1><div id="quotegot" class="well well-lg quote"></div></h1>
<button type="submit" class="btn btn-success btn-lg" id="quoteid">Get Quote</button>
<a href="https://twitter.com/share" class="twitter-share-button" data-show-count="false" data-text="hello">Tweet</a><script async src="//platform.twitter.com/widgets.js" charset="utf-8"></script>
</body>
JS:
$("#quoteid").on("click",function(){
$.getJSON("http://api.forismatic.com/api/1.0/?method=getQuote&key=457653&format=json&lang=en",function(quotejson){
$("#quotegot").html(quotejson["quoteText"]);
});
});
$(".twitter-share-button").attr("data-text", $(".quote").text());
無法發表評論,因此將其放在此處。 那里有一個jsfiddle顯示它是如何工作的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.