将项目从JavaScript添加到HTML列表中。

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

Adding items to a html list from javascript

问题

我正在尝试将JavaScript数组中的项目添加到我的HTML列表中。我已经成功使数组显示出来,但是整个数组显示在列表中的一个项目中,而不是每个项目都有自己的项目符号。

例如:

•Sugar,Milk,Bread,Apples

而不是:

•Sugar
•Milk
•Bread
•Apples
let myArray = ["Sugar", "Milk", "Bread", "Apples"]

arrayList = (arr) => {
    let list1 = document.querySelector("#itemList");
    arr.forEach(item => {
        let myListItems = document.createElement("li");
        myListItems.textContent = item;
        list1.appendChild(myListItems);
    });
}

arrayList(myArray)
英文:

I am trying to add items from my array in javascript into my html list. I have gotten it where the array displays but my entire array displays in one bullet point on my list instead of each item on their own bullet points.

eg

•Sugar,Milk,Bread,Apples

instead of

•Sugar
•Milk
•Bread
•Apples

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

let myArray = [&quot;Sugar&quot;, &quot;Milk&quot;, &quot;Bread&quot;, &quot;Apples&quot;]

arrayList = (arr) =&gt; {
    let list1 = document.querySelector(&quot;#itemList&quot;);
    let myListItems = document.createElement(&quot;li&quot;);
    myListItems.textContent = arr;
    list1.appendChild(myListItems)
}

arrayList(myArray)

答案1

得分: 2

问题出在你获取了所有数组元素的文本,并将它们放入单个li中。

要解决这个问题,你需要遍历数组,为其中的每个项创建一个新的li

let myArray = ["Sugar", "Milk", "Bread", "Apples"];
let list1 = document.querySelector("#itemList");

arrayList = (arr) => {
  let items = arr.forEach(item => {
    let li = document.createElement('li');
    li.textContent = item;
    list1.appendChild(li)
  });
}

arrayList(myArray)
<ul id="itemList"></ul>
英文:

The issue is because you're getting the text of all the array elements and putting them in to a single li.

To fix the problem you need to loop through the array, creating a new li for each item within it:

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

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

let myArray = [&quot;Sugar&quot;, &quot;Milk&quot;, &quot;Bread&quot;, &quot;Apples&quot;];
let list1 = document.querySelector(&quot;#itemList&quot;);

arrayList = (arr) =&gt; {
  let items = arr.forEach(item =&gt; {
    let li = document.createElement(&#39;li&#39;);
    li.textContent = item;
    list1.appendChild(li)
  });
}

arrayList(myArray)

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

&lt;ul id=&quot;itemList&quot;&gt;&lt;/ul&gt;

<!-- end snippet -->

答案2

得分: 1

你需要循环遍历数组:

<html>
<body>
<ul id="itemList">
</ul>
<script>
const myArray = ["Sugar", "Milk", "Bread", "Apples"]

createLiAndAppend = (arr, listId) => {
    const list1 = document.getElementById(listId);
    const myListItems = document.createElement("li");
    myListItems.textContent = arr;
    list1.appendChild(myListItems);
}

myArray.forEach(m => createLiAndAppend(m, "itemList"));
</script>
</body>
</html>

如果需要进一步的帮助,请告诉我。

英文:

You need to loop through the array:

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

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

&lt;html&gt;
&lt;body&gt;
&lt;ul id=&quot;itemList&quot;&gt;
&lt;/ul&gt;
&lt;/body&gt;
&lt;script&gt;
const myArray = [&quot;Sugar&quot;, &quot;Milk&quot;, &quot;Bread&quot;, &quot;Apples&quot;]

createLiAndAppend = (arr, listId) =&gt; {
    const list1 = document.getElementById(listId);
    const myListItems = document.createElement(&quot;li&quot;);
    myListItems.textContent = arr;
    list1.appendChild(myListItems);
}

myArray.forEach(m =&gt; createLiAndAppend(m, &quot;itemList&quot;));
&lt;/script&gt;
&lt;/html&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月1日 17:15:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76380371.html
匿名

发表评论

匿名网友

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

确定