使用jquery基于来自API的输入数据动态更改Twitter按钮的data-text属性的值

时间:2022-11-27 19:36:51

I designed a page, where clicking on the button "Get Quote" will display various quotes in a div, which are obtained from an API. I want to add a twitter shate button. But I am really confused on how to change the value of the "data-text" attribute of the twitter button, dynamically which the changing values of the quotes from the API. I use jQuery for processing data from the API, so how do I change the value of the "data-text" using jQuery?

我设计了一个页面,点击“获取引用”按钮将在div中显示各种引号,这些引用是从API获得的。我想添加一个twitter shate按钮。但我真的很困惑如何更改twitter按钮的“data-text”属性的值,动态地改变API中引号的值。我使用jQuery处理来自API的数据,那么如何使用jQuery更改“数据文本”的值?

The code is:

代码是:

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());

1 个解决方案

#1


0  

Dynamically change Tweet Button "data-text" contents

动态更改Tweet按钮“数据文本”内容

Can't comment yet so putting it here. There is a jsfiddle in there showing how it works.

无法评论,所以把它放在这里。那里有一个jsfiddle显示它是如何工作的。

#1


0  

Dynamically change Tweet Button "data-text" contents

动态更改Tweet按钮“数据文本”内容

Can't comment yet so putting it here. There is a jsfiddle in there showing how it works.

无法评论,所以把它放在这里。那里有一个jsfiddle显示它是如何工作的。