简体   繁体   English

使用JS导入Json数据后加载页面后显示默认结果

[英]Show default result once the page is loaded after importing Json data with JS

I've been trying to import Json data from here https://jsonplaceholder.typicode.com/posts/1 to my website. 我一直在尝试从这里https://jsonplaceholder.typicode.com/posts/1导入Json数据到我的网站。

I just managed to do that but now I'd like to create different buttons connected to different posts, so: 我只是设法做到这一点,但现在我想创建连接到不同帖子的不同按钮,因此:

and so on. 等等。 I order to do that I copied the function I used for the first one and I reused the function by changing the function's name. 我要做的是,我复制了用于第一个函数的函数,并通过更改函数名称重用了该函数。 I don't know if it was a good idea or if there is a better way to do that and avoid copying the function every time I have to add a post. 我不知道这是一个好主意还是有更好的方法来做到这一点,并且避免在每次必须添加帖子时都复制该函数。

At the moment seems to work but every time I load the page I get a random post. 目前似乎可以正常工作,但是每次我加载页面时,都会收到一个随机帖子。 I'd like to show the first one ( https://jsonplaceholder.typicode.com/posts/1 ) and show the others just if I click on the buttons (numbers), is there a way to do that? 我想显示第一个( https://jsonplaceholder.typicode.com/posts/1 )并显示其他(仅当我单击按钮(数字)时),有没有办法做到这一点?

Here my code: 这是我的代码:

HTML 的HTML

 <div class="news-wrap margin-top-div">
        <div class="news-btn-container-flex">
          <div class="news-btn-div current" data-tab="1" onclick="req1()">1</div>
          <div class="news-btn-div" data-tab="2" onclick="req2()">2</div>
          <div class="news-btn-div" data-tab="3" onclick="req3()">3</div>
          <div class="news-btn-div" data-tab="4" onclick="req4()">4</div>
          <div class="news-btn-div" data-tab="5" onclick="req5()">5</div>       
        </div>

        <div class="news-content-container-flex">
          <div class="news-title">
            <span id="newsTitle">
            </span>
          </div>
          <div class="news-content-1">
            <span id="newsContent">              
            </span>
          </div>
          <div class="news-content-2">
            <span>
            </span>
          </div>
        </div>       

      </div>

JS JS

function req1() {
  fetch('https://jsonplaceholder.typicode.com/posts/1')
    .then(response => response.json())
    .then(json => {
      const title = json.title;
      const body = json.body;
      document.getElementById("newsTitle").innerHTML = title;
      document.getElementById("newsContent").innerHTML = body;
    });
}
req1();


function req2() {
  fetch('https://jsonplaceholder.typicode.com/posts/2')
    .then(response => response.json())
    .then(json => {
      const title = json.title;
      const body = json.body;
      document.getElementById("newsTitle").innerHTML = title;
      document.getElementById("newsContent").innerHTML = body;
    });
}
req2();


function req3() {
  fetch('https://jsonplaceholder.typicode.com/posts/3')
    .then(response => response.json())
    .then(json => {
      const title = json.title;
      const body = json.body;
      document.getElementById("newsTitle").innerHTML = title;
      document.getElementById("newsContent").innerHTML = body;
    });
}
req3();


function req4() {
  fetch('https://jsonplaceholder.typicode.com/posts/4')
    .then(response => response.json())
    .then(json => {
      const title = json.title;
      const body = json.body;
      document.getElementById("newsTitle").innerHTML = title;
      document.getElementById("newsContent").innerHTML = body;
    });
}
req4();


function req5() {
  fetch('https://jsonplaceholder.typicode.com/posts/5')
    .then(response => response.json())
    .then(json => {
      const title = json.title;
      const body = json.body;
      document.getElementById("newsTitle").innerHTML = title;
      document.getElementById("newsContent").innerHTML = body;
    });
}
req5();

Here my JSfiddle where posts are shown randomly when the page is loaded 这是我的JSfiddle,页面加载时随机显示帖子

https://jsfiddle.net/matteous1/ywh0spga/ https://jsfiddle.net/matteous1/ywh0spga/

First unique your req function to this 首先使您的req功能独特

function req(input) {
  fetch('https://jsonplaceholder.typicode.com/posts/' + input)
    .then(response => response.json())
    .then(json => {
      return json;
    });
}

Then, You must use promise . 然后,您必须使用promise Read more about Promise here 在此处阅读有关Promise的更多信息

Promise.all([req(1), req(2), req(3)])
    .then(function(res) {
        // all data here
    }, function(err) {
        // one or more failed
    });

And put different elements and tags for each post or make them dynamically. 并为每个帖子放置不同的元素和标签,或者使它们动态化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM