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

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

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>

  1. &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;

  1. // titleinput.textContent = ;
  2. document.getElementById(&quot;mydes&quot; + number).innerHTML = image;
  3. document.getElementById(&quot;myLink&quot; + number).href = organicUrl;
  4. document.getElementById(&quot;myLink&quot; + number).innerHTML = organicUrl;
  5. number++;
  6. }

})

如何从响应数组 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.

  1. &lt;div class=&quot;inside&quot;&gt;
  2. &lt;a id=&quot;myLink5&quot; href=&quot;https://www.example.com&quot;&gt;Example Link&lt;/a&gt;
  3. &lt;h1 id=&quot;titleinput5&quot;&gt;&lt;/h1&gt;
  4. &lt;div&gt;
  5. &lt;p id=&quot;mydes5&quot;&gt; &lt;/p&gt;
  6. &lt;/div&gt;
  7. &lt;/div&gt;

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

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

  1. var input = document.getElementById(&quot;searchbartext&quot;);
  2. var inputValue = input.value;
  3. const url2 = &#39;https://api.bing.microsoft.com/v7.0/search?q=&#39; + inputValue + &#39;&amp;count=50&#39;;
  4. // totalEstimatedMatches
  5. console.log(url2);
  6. fetch(url2, options)
  7. .then(response =&gt; response.json())
  8. .then(data =&gt; {
  9. console.log(data);
  10. element.style.display = &#39;block&#39;;
  11. var number = 1;
  12. for (var i = 0; i &lt; 7; i++) {
  13. const title = data.webPages.value[i].name;
  14. const organicUrl = data.webPages.value[i].url;
  15. const image = data.webPages.value[i].snippet;
  16. const totalEstimatedMatches = data.webPages.totalEstimatedMatches;
  17. document.getElementById(&quot;number_of_results&quot;).innerHTML = totalEstimatedMatches;
  18. document.getElementById(&quot;titleinput&quot; + number).innerHTML = title;
  19. // titleinput.textContent = ;
  20. document.getElementById(&quot;mydes&quot; + number).innerHTML = image;
  21. document.getElementById(&quot;myLink&quot; + number).href = organicUrl;
  22. document.getElementById(&quot;myLink&quot; + number).innerHTML = organicUrl;
  23. number++;
  24. }
  25. })

<!-- end snippet -->

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

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

答案1

得分: 1

使用document.createElement

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

  1. const container = document.getElementById('result');
  2. const count = document.getElementById('count');
  3. // API响应数据示例
  4. const webPages ={
  5. totalEstimatedMatches:789456123,
  6. value:[
  7. {
  8. id:'https://example.com',
  9. name:'Example Website',
  10. url:'https://google.com',
  11. snippet:'Lorem iseuj something'
  12. },
  13. // 其他数据...
  14. ]
  15. }
  16. // 在循环外部使用count
  17. count.innerText = webPages.totalEstimatedMatches;
  18. for (var i = 0; i < 7; i++) {
  19. const title = webPages.value[i].name;
  20. const organicUrl = webPages.value[i].url;
  21. const des = webPages.value[i].snippet;
  22. const totalEstimatedMatches = webPages.totalEstimatedMatches;
  23. let div = document.createElement('div');
  24. let a = document.createElement('a');
  25. let h1 = document.createElement('h1');
  26. let p = document.createElement('p');
  27. a.href = organicUrl;
  28. a.innerText = organicUrl;
  29. h1.innerText = title;
  30. p.innerText = des;
  31. div.append(a, h1, p);
  32. container.append(div);
  33. }
  1. <section>
  2. <span>Count</span><h5 id='count'></h5>
  3. <main id='result' class='result'>
  4. </main>
  5. </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 -->

  1. const container = document.getElementById(&#39;result&#39;);
  2. const count = document.getElementById(&#39;count&#39;);
  3. //api response data sample
  4. const webPages ={
  5. totalEstimatedMatches:789456123,
  6. value:[
  7. {
  8. id:&#39;https://example.com&#39;,
  9. name:&#39;Example Website&#39;,
  10. url:&#39;https://google.com&#39;,
  11. snippet:&#39;Lorem iseuj something&#39;
  12. },
  13. {
  14. id:&#39;https://example.com&#39;,
  15. name:&#39;Example Website&#39;,
  16. url:&#39;https://google.com&#39;,
  17. snippet:&#39;Lorem iseuj something&#39;
  18. },
  19. {
  20. id:&#39;https://example.com&#39;,
  21. name:&#39;Example Website&#39;,
  22. url:&#39;https://google.com&#39;,
  23. snippet:&#39;Lorem iseuj something&#39;
  24. },
  25. {
  26. id:&#39;https://example.com&#39;,
  27. name:&#39;Example Website&#39;,
  28. url:&#39;https://google.com&#39;,
  29. snippet:&#39;Lorem iseuj something&#39;
  30. },
  31. {
  32. id:&#39;https://example.com&#39;,
  33. name:&#39;Example Website&#39;,
  34. url:&#39;https://google.com&#39;,
  35. snippet:&#39;Lorem iseuj something&#39;
  36. },
  37. {
  38. id:&#39;https://example.com&#39;,
  39. name:&#39;Example Website&#39;,
  40. url:&#39;https://google.com&#39;,
  41. snippet:&#39;Lorem iseuj something&#39;
  42. },
  43. {
  44. id:&#39;https://example.com&#39;,
  45. name:&#39;Example Website&#39;,
  46. url:&#39;https://google.com&#39;,
  47. snippet:&#39;Lorem iseuj something&#39;
  48. },
  49. {
  50. id:&#39;https://example.com&#39;,
  51. name:&#39;Example Website&#39;,
  52. url:&#39;https://google.com&#39;,
  53. snippet:&#39;Lorem iseuj something&#39;
  54. },
  55. {
  56. id:&#39;https://example.com&#39;,
  57. name:&#39;Example Website&#39;,
  58. url:&#39;https://google.com&#39;,
  59. snippet:&#39;Lorem iseuj something&#39;
  60. }
  61. ]
  62. }
  63. //count use outside the loop
  64. count.innerText = webPages.totalEstimatedMatches;
  65. for (var i = 0; i &lt; 7; i++) {
  66. const title = webPages.value[i].name;
  67. const organicUrl = webPages.value[i].url;
  68. const des = webPages.value[i].snippet;
  69. const totalEstimatedMatches = webPages.totalEstimatedMatches;
  70. let div = document.createElement(&#39;div&#39;);
  71. let a = document.createElement(&#39;a&#39;);
  72. let h1 = document.createElement(&#39;h1&#39;);
  73. let p = document.createElement(&#39;p&#39;);
  74. a.href = organicUrl;
  75. a.innerText = organicUrl;
  76. h1.innerText = title;
  77. p.innerText = des
  78. div.append(a,h1,p);
  79. container.append(div)
  80. }

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

  1. &lt;section&gt;
  2. &lt;span&gt;Count&lt;/span&gt;&lt;h5 id=&#39;count&#39;&gt;&lt;/h5&gt;
  3. &lt;main id=&#39;result&#39; class=&#39;result&#39;&gt;
  4. &lt;/main&gt;
  5. &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:

确定