亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用 for 循環從 JSON URL 打印所有數據?

如何使用 for 循環從 JSON URL 打印所有數據?

HUWWW 2022-06-05 11:10:49
當我嘗試在 HTML 上輸出它時,它不會顯示整個輸出,而是一個一個國家。我嘗試使用它來輸出它,document.write但它會覆蓋整個 HTML。如何在不替換所有 HTML 代碼的情況下打印所有數據?我還嘗試了其他方式來使用getElementById,輸出它innerHTML。這些都不起作用。<!DOCTYPE html><html><head>  <link rel="stylesheet" href="https://unpkg.com/onsenui/css/onsenui.css">  <link rel="stylesheet" href="https://unpkg.com/onsenui/css/onsen-css-components.min.css">  <script src="https://unpkg.com/onsenui/js/onsenui.min.js"></script>  <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script></head><body style="color:silver;">  <ons-page>    <ons-toolbar>      <div class="center" style="color:#fff; text-shadow: 1px 1px silver; background-color:#E20000;">CORONA VIRUS TRACKER</div>    </ons-toolbar>    <ons-tabbar swipeable position="auto">      <ons-tab page="tab1.html" label="Global" icon="globe, material:md-home" active>      </ons-tab>    </ons-tabbar>  </ons-page>  <template id="tab1.html">  <ons-page id="Tab1">   <div class="allcountry"></div>  </ons-page></template>  <script>    for (var i = 0; i < 199; i++) {      (function(i) {        $.getJSON('https://coronavirus-19-api.herokuapp.com/countries', function(data3) {          var txt = `<table style="border-collapse: collapse; font-family: Andale Mono, monospace; border: silver;" width="100%" border="1">        <tr><td><div style="background-color: #EB0D0D; text-transform: uppercase; text-align:center; color:#fff;">${data3[i].country}<br></div></td></tr>                               <tr><td><img src="https://via.placeholder.com/150" height="90" width="90" style="float: left; padding-right: 5px;"><h5>CONFIRMED CASES</h5> <h3>${data3[i].cases}</h3></td></tr>                                        <tr><td><img src="https://via.placeholder.com/150" height="90" width="90" style="float: left; padding-right: 5px;"><h5>NEW CASES</h5> <h3>${data3[i].todayCases}</h3></td></tr>                                               $(".allcountry").html(txt);        })      })(i);    }  </script></body></html>
查看完整描述

1 回答

?
慕容森

TA貢獻1853條經驗 獲得超18個贊

txt在開始 for 循環之前定義,并在循環完成后顯示值。


var txt = '';


for (var i = 0; i < 199; i++) {

    // Concatenate the values to txt here

    txt += '<table....';

}


$(".allcountry").html(txt);


查看完整回答
反對 回復 2022-06-05
  • 1 回答
  • 0 關注
  • 106 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號