请帮助我在初始页面加载后刷新单个 html 元素。我的程序在基本方面应该做什么:(FreeCodeCamp 练习:随机报价机)
- 从 API 获取随机报价
- 显示报价和作者
- 获取新报价/作者的按钮
- 用于推文引用/作者的按钮
我已经成功地让一切正常工作,但我发现我用来发送推文的锚元素在初始页面加载后不会刷新。它的“数据文本”属性已相应更改,但它并未反映在按钮/链接上。
这是我的 html 正文的摘录:http ://codepen.io/jattas/pen/gmNQpv 上的完整代码
<script src="https://code.jquery.com/jquery-3.2.1.js"></script>
<div class="button-box">
<button id="getQuote">Generate new quote</button>
<a href="https://twitter.com/share" class="twitter-share-button" data-text="" data-show-count="false">Tweet</a>
<script async src="//platform.twitter.com/widgets.js" charset="utf-8"></script>
</div>
我需要刷新锚元素以显示新更改的属性,而不是初始页面加载中的属性。请帮忙!我敢肯定这不应该那么困难。
这是我的 javascript 中的相关摘录:
var textToTweet = '';
function processQuote() {
var currentQuote = obj.quote;
var currentAuthor = obj.author;
$(".quote").html(currentQuote);
$(".author").html("- " + currentAuthor);
textToTweet = currentQuote + " " + "-" + currentAuthor;
$(".twitter-share-button").attr("data-text", textToTweet);
alert($(".twitter-share-button").attr("data-text")); //this is to confirm that the "data-text" attribute has changed as planned
}
$(document).ready(function() {
processQuote();
$("#getQuote").on("click", function() {
getQuote();
processQuote();
});
});
非常感谢。