我正在为freeCodeCamp和使用codepen做一个“随机报价机”挑战。对我来说,jQuery部分是难以解决的问题。除了两个问题外,Mi代码运行良好:
我的JS文件:
$(document).ready(function() {
// Initial quote not working
let initQuote = $.getJSON("https://quotesondesign.com/wp-json/posts?filter[orderby]=rand&callback=", function (data) {
$(".message").html(data[0].content + " — " + data[0].title);
});
$.(".message").append(initQuote);
// Get quote button working ok
$(".btn-quote").on("click", function() {
$.ajaxSetup({ cache: false });
$.getJSON(
"https://quotesondesign.com/wp-json/posts?filter[orderby]=rand&callback=",
function(data) {
$(".message").html(data[0].content + " — " + data[0].title);
}
);
});
// Stuck on tweet button, not working
$(".btn-twitter").on('click', function (event) {
event.preventDefault();
window.open('https://www.twitter.com/intent/tweet?text=' + encodeURIComponent( + ' --'))
});
});
你可以看到我的笔here
欢迎任何建议
答案 0 :(得分:1)
获取初始引用是一个异步调用,因此回调将在>>附加>>后运行:
gdate2 = GROUP removeDash by (day,hour);
minTemps = FOREACH gdate2 GENERATE FLATTEN(group) as (day,hour),MIN(removeDash.temp) as minTemp;
这是有效的。
你的Twitter按钮非常接近。您只需添加引号:// This will run before your network request returns
`$.(".message").append(initQuote);`
// Then the network request will finish, and call this callback to set the content:
let initQuote = $.getJSON("https://quotesondesign.com/wp-json/posts?filter[orderby]=rand&callback=", function (data) {
$(".message").html(data[0].content + " — " + data[0].title);
});
$(".message").text()