-1
下面的代碼應該顯示一個隨機引用。但它什麼都沒有返回。只顯示html頁面佈局。爲什麼下面的JavaScript回調函數不工作:回調函數在下面的代碼中不起作用?
$(document).ready(function() {
function cb() {
var addr = "http://quotesondesign.com/wp-json/posts?filter[orderby]=rand&filter[posts_per_page]=1";
$.getJSON(addr, function(rslt) {
return rslt;
});
};
function qte(rslt) {
$("#qti").html(rslt[0].content);
$("#athr").html(rslt[0].title);
};
qte(cb);
$("#nqt").on("click", function() {
qte(cb);
});
$("#tit").on("click", function() {
window.open("https://twitter.com/intent/tweet?text=" + encodeURIComponent(qwe));
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container-fluid">
<h1 class="text-center" style="margin-top:20px;">Random Quote Generator</h1>
<div class="box">
<div class="qt">
<span id="qti"></span>
</div>
<div class="athr">- <span id="athr"></span></div>
<div style="width:100%; clear:both; margin-left:auto; margin-right:auto; margin-top:6%;">
<button class="btn" title="twitt it!" id="tit"><i class="fa fa-twitter"></i></button>
<button class="btn pull-right" id="nqt">New Quote</button>
</div>
</div>
</div>
的回調的getJSON是異步調用的,所以你的回調是沒有做任何事情。 – jdigital
你知道你的cb函數沒有返回任何東西嗎? –
你的瀏覽器有一個內置的調試器。對於深入瞭解這類問題非常有幫助。我建議花時間學習使用它。 – jdigital