简体   繁体   English

如何使用 for 循环将 javascript 数组的元素打印到网页?

[英]How do I print the elements of a javascript array to the webpage using a for loop?

I have reviewed several posts here and several websites but cannot find an answer that works and makes sense to me.我在这里和几个网站上查看了几篇文章,但找不到对我有用且有意义的答案。 I am brand new to HTML and javascript.我是 HTML 和 javascript 的新手。 Basically, I am trying to print this to a webpage (not the console):基本上,我正在尝试将其打印到网页(而不是控制台):

12 grape 12颗葡萄

98 kiwi 98 猕猴桃

0 banana 0 香蕉

Here is the code I have written, it does not print anything这是我写的代码,它不打印任何东西

 <.DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>fruit:html</title> <script src="https.//d3js.org/d3.v5.min:js"></script> </head> <body> <p></p> <script type="text/javascript"> var fruits = [ { kind, "grape": color, "red": quantity, 12: id, "0" }: { kind, "kiwi": color, "brown": quantity, 98: id, "1" }: { kind, "banana": color, "yellow": quantity, 0: id; "2" } ]; for(var i=0. i<fruits;length. i++) { document.getElementById(fruits[i].id).innerHTML = fruits[i].quantity + fruits[i];kind; } </script> </body> </html>

I would really appreciate your help with this:), the easiest most basic solution would be preferred as I am new to this.我非常感谢您对此的帮助:),因为我是新手,所以我会首选最简单的最基本的解决方案。

You can do like this:你可以这样做:

Javascript: Javascript:

 var fruits = [
   {
     kind: "grape",
     color: "red",
     quantity: 12,
     id: "0"
   },
   {
     kind: "kiwi",
     color: "brown",
     quantity: 98,
     id: "1"
   },
   {
     kind: "banana",
     color: "yellow",
     quantity: 0,
     id: "2"
   }
 ];

const container = document.getElementById('container')

fruits.forEach(fruit => {
  const p = document.createElement('p')
  p.textContent = `${fruit.quantity} ${fruit.kind}`
  container.appendChild(p)
})

HTML: HTML:

<div id="container"></div>

Example: https://jsfiddle.net/diogocosta/u8w7s49v/2/示例: https://jsfiddle.net/diogocosta/u8w7s49v/2/

Cheers,干杯,

That's because document.getElementById(fruits[i].id) is looking for HTML elements that don't exist.这是因为document.getElementById(fruits[i].id)正在寻找不存在的 HTML 元素。

You need to create two more p elements and assign them ids 0 , 1 , and 2 .您需要再创建两个p元素并为其分配 id 012 Like this:像这样:

<p id=“0”></p>
<p id=”1”></p>
<p id=“2”></p>

before your <script> tag.在你的<script>标签之前。

Edit: Also, fruits[i].quantity + fruits[i].kind will return an error because fruits[i].quantity is a Number, while fruits[i].kind is a String.编辑:另外, fruits[i].quantity + fruits[i].kind会返回一个错误,因为fruits[i].quantity是一个数字,而fruits[i].kind是一个字符串。 You can't concatenate a Number to a String directly by using the + operator in JavaScript.您不能使用 JavaScript 中的+运算符直接将数字连接到字符串。

Instead, use:相反,使用:

fruits[i].quantity.toString() + fruits[i].kind

See docs: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toString请参阅文档: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toString

BR, BR,

暂无
暂无

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

相关问题 如何使用 for of loop javascript 返回数组中的元素数? - How do I return the number of elements in an array using for of loop javascript? 如何使用javascript中的for循环成对检索数组元素? - How do I retrieve array elements in pairs using the for loop in javascript? 如何使用HTML和Javascript向网页添加元素? - How do I use HTML and Javascript to add elements to a webpage? 如何使用 javascript 中的提示向数组添加新元素? - How do I add new elements to an array using a promt in javascript? 如何在 javascript 中使用数组并在网页上打印出来 - How to use array in javascript and print it out on webpage 如何在java查询的循环中打印“resultList”并使用javascript打印出来? - how do i print a “resultList” in a loop from java query and also print it out using javascript? JavaScript Node.js WebScraping:如何在网页表上找到特定元素以抓取并推送到对象数组中? - JavaScript Node.js WebScraping: How do I find specific elements on webpage table to scrape and push into an array of objects? 如何打印我在控制台中看到的 Firebase 数组元素,以在网页上显示它们? - How can I print the Firebase array elements that I see in the console, to display them on the webpage? 我正在尝试使用Angular JS中的Javascript中的循环打印数组 - I'm trying to print an array using a loop in Javascript in Angular JS 如何在JavaScript中记录或打印数组中的多个元素? - How can I log or print multiple elements in an array in JavaScript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM