如何从响应数组 JSON 动态向 HTML 页面插入数据?

huangapple go评论70阅读模式
英文:

how can I dynamically insert data to html page from response array json?

问题

我正在制作一个Google搜索引擎克隆,有一个结果数组。我正在运行一个for循环,将数据输入到HTML页面中,并对ID进行编号,同时递增它们。虽然这个解决方案有效,但不是最优解。我如何根据结果数量动态插入数据?我正在使用原生JavaScript。

<div class="inside">
<a id="myLink5" href="https://www.example.com">示例链接</a>
<h1 id="titleinput5"></h1>
<div>

&lt;p id=&quot;mydes5&quot;&gt; &lt;/p&gt;

</div>

</div>


var input = document.getElementById("searchbartext");
var inputValue = input.value;
const url2 = 'https://api.bing.microsoft.com/v7.0/search?q=' + inputValue + '&count=50';

// totalEstimatedMatches

console.log(url2);
fetch(url2, options)
.then(response => response.json())
.then(data => {
console.log(data);
element.style.display = 'block';
var number = 1;
for (var i = 0; i < 7; i++) {
const title = data.webPages.value[i].name;
const organicUrl = data.webPages.value[i].url;
const image = data.webPages.value[i].snippet;
const totalEstimatedMatches = data.webPages.totalEstimatedMatches;
document.getElementById("number_of_results").innerHTML = totalEstimatedMatches;
document.getElementById("titleinput" + number).innerHTML = title;

  //    titleinput.textContent = ;
  document.getElementById(&quot;mydes&quot; + number).innerHTML = image;
  document.getElementById(&quot;myLink&quot; + number).href = organicUrl;
  document.getElementById(&quot;myLink&quot; + number).innerHTML = organicUrl;
  number++;
}

})

如何从响应数组 JSON 动态向 HTML 页面插入数据?

如何从响应数组 JSON 动态向 HTML 页面插入数据?

英文:

I am working on a Google search engine clone and have an array of results. I am running a for loop to enter the data into an HTML page, and I have numbered the IDs while incrementing them. While this solution works, it is not optimal. How can I dynamically insert data based on the number of results? I am using vanilla JavaScript.

&lt;div class=&quot;inside&quot;&gt;
  &lt;a id=&quot;myLink5&quot; href=&quot;https://www.example.com&quot;&gt;Example Link&lt;/a&gt;
  &lt;h1 id=&quot;titleinput5&quot;&gt;&lt;/h1&gt;
  &lt;div&gt;

    &lt;p id=&quot;mydes5&quot;&gt; &lt;/p&gt;

  &lt;/div&gt;

&lt;/div&gt;

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

var input = document.getElementById(&quot;searchbartext&quot;);
var inputValue = input.value;
const url2 = &#39;https://api.bing.microsoft.com/v7.0/search?q=&#39; + inputValue + &#39;&amp;count=50&#39;;

// totalEstimatedMatches

console.log(url2);
fetch(url2, options)
  .then(response =&gt; response.json())
  .then(data =&gt; {
    console.log(data);
    element.style.display = &#39;block&#39;;
    var number = 1;
    for (var i = 0; i &lt; 7; i++) {
      const title = data.webPages.value[i].name;
      const organicUrl = data.webPages.value[i].url;
      const image = data.webPages.value[i].snippet;
      const totalEstimatedMatches = data.webPages.totalEstimatedMatches;
      document.getElementById(&quot;number_of_results&quot;).innerHTML = totalEstimatedMatches;
      document.getElementById(&quot;titleinput&quot; + number).innerHTML = title;


      //    titleinput.textContent = ;
      document.getElementById(&quot;mydes&quot; + number).innerHTML = image;
      document.getElementById(&quot;myLink&quot; + number).href = organicUrl;
      document.getElementById(&quot;myLink&quot; + number).innerHTML = organicUrl;
      number++;
    }
  })

<!-- end snippet -->

如何从响应数组 JSON 动态向 HTML 页面插入数据?

如何从响应数组 JSON 动态向 HTML 页面插入数据?

答案1

得分: 1

使用document.createElement

下面我使用了示例响应数据,您可以在fetch方法中使用其中的逻辑。
还要遵循HTML结构。

const container = document.getElementById('result');
const count = document.getElementById('count');
// API响应数据示例
const webPages ={
  totalEstimatedMatches:789456123,
  value:[
    {
      id:'https://example.com',
      name:'Example Website',
      url:'https://google.com',
      snippet:'Lorem iseuj something'
    },
    // 其他数据...
  ]
}

// 在循环外部使用count
count.innerText = webPages.totalEstimatedMatches;

for (var i = 0; i < 7; i++) {
      const title = webPages.value[i].name;
      const organicUrl = webPages.value[i].url;
      const des = webPages.value[i].snippet;
      const totalEstimatedMatches = webPages.totalEstimatedMatches;
      let div = document.createElement('div');
      let a = document.createElement('a');
      let h1 = document.createElement('h1');
      let p = document.createElement('p');
      a.href = organicUrl;
      a.innerText = organicUrl;
      h1.innerText = title;
      p.innerText = des;
      div.append(a, h1, p);
      
      container.append(div);
}
<section>
  <span>Count</span><h5 id='count'></h5>
  <main id='result' class='result'>
  
  </main>
</section>

请注意,上述代码是JavaScript和HTML的混合代码,用于创建和填充HTML元素。

英文:

Use document.createElement

Below i have used sample response data, you can use the logic inside your fetch method.
Also follow the html structure

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const container = document.getElementById(&#39;result&#39;);
const count = document.getElementById(&#39;count&#39;);
//api response data sample
const webPages ={
  totalEstimatedMatches:789456123,
  value:[
    {
      id:&#39;https://example.com&#39;,
      name:&#39;Example Website&#39;,
      url:&#39;https://google.com&#39;,
      snippet:&#39;Lorem iseuj something&#39;
    },
    {
      id:&#39;https://example.com&#39;,
      name:&#39;Example Website&#39;,
      url:&#39;https://google.com&#39;,
      snippet:&#39;Lorem iseuj something&#39;
    },
    {
      id:&#39;https://example.com&#39;,
      name:&#39;Example Website&#39;,
      url:&#39;https://google.com&#39;,
      snippet:&#39;Lorem iseuj something&#39;
    },
    {
      id:&#39;https://example.com&#39;,
      name:&#39;Example Website&#39;,
      url:&#39;https://google.com&#39;,
      snippet:&#39;Lorem iseuj something&#39;
    },
    {
      id:&#39;https://example.com&#39;,
      name:&#39;Example Website&#39;,
      url:&#39;https://google.com&#39;,
      snippet:&#39;Lorem iseuj something&#39;
    },
    {
      id:&#39;https://example.com&#39;,
      name:&#39;Example Website&#39;,
      url:&#39;https://google.com&#39;,
      snippet:&#39;Lorem iseuj something&#39;
    },
    {
      id:&#39;https://example.com&#39;,
      name:&#39;Example Website&#39;,
      url:&#39;https://google.com&#39;,
      snippet:&#39;Lorem iseuj something&#39;
    },
    {
      id:&#39;https://example.com&#39;,
      name:&#39;Example Website&#39;,
      url:&#39;https://google.com&#39;,
      snippet:&#39;Lorem iseuj something&#39;
    },
    {
      id:&#39;https://example.com&#39;,
      name:&#39;Example Website&#39;,
      url:&#39;https://google.com&#39;,
      snippet:&#39;Lorem iseuj something&#39;
    }
  ]
}

//count use outside the loop

count.innerText = webPages.totalEstimatedMatches;

for (var i = 0; i &lt; 7; i++) {
      const title = webPages.value[i].name;
      const organicUrl = webPages.value[i].url;
      const des = webPages.value[i].snippet;
      const totalEstimatedMatches = webPages.totalEstimatedMatches;
      let div = document.createElement(&#39;div&#39;);
      let a = document.createElement(&#39;a&#39;);
      let h1 = document.createElement(&#39;h1&#39;);
      let p = document.createElement(&#39;p&#39;);
      a.href = organicUrl;
      a.innerText = organicUrl;
      h1.innerText = title;
      p.innerText = des
      div.append(a,h1,p);
      
      container.append(div)

}

<!-- language: lang-html -->

&lt;section&gt;
&lt;span&gt;Count&lt;/span&gt;&lt;h5 id=&#39;count&#39;&gt;&lt;/h5&gt;
&lt;main id=&#39;result&#39; class=&#39;result&#39;&gt;
  
&lt;/main&gt;
&lt;/section&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月10日 12:11:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76214830.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定