简体   繁体   English

来自数组的 Javascript 无序列表

[英]Javascript Unordered List from Array

I just came across this on the w3schools Javascript tutorial:我刚刚在 w3schools Javascript 教程中遇到了这个:

var fruits, text, fLen, i;
fruits = ["Banana", "Orange", "Apple", "Mango"];
fLen = fruits.length;

text = "<ul>";
for (i = 0; i < fLen; i++) {
  text += "<li>" + fruits[i] + "</li>";
}
text += "</ul>";

I understand what arrays are and how to loop through them, but I need help with the part where they are creating the unordered list from the array.我了解什么是数组以及如何遍历它们,但我需要帮助他们从数组创建无序列表的部分。 Can anyone help with this please?任何人都可以帮忙吗?

Thanks in advance, Wasi提前致谢,瓦西

That code doesn't create an unordered list from an array.该代码不会从数组创建无序列表。 It creates a string containing HTML markup for one, by starting out with a string:它创建一个包含 HTML 标记的字符串,从一个字符串开始:

text = "<ul>";

then adding to it once for each entry in the string:然后为字符串中的每个条目添加一次:

text += "<li>" + fruits[i] + "</li>";
//   ^^-- adds to the end of the string (well, technically it creates a new string
//        and updates `text` to contain the new string)

then adding the ending </ul> after the loop:然后在循环后添加结尾</ul>

text += "</ul>";

At the end, text is a string with this HTML in it:最后, text是一个包含此 HTML 的字符串:

<ul><li>Banana</li><li>Orange</li><li>Apple</li><li>Mango</li></ul>

To actually create an unordered list, something has to parse that HTML.要实际创建一个无序列表,必须解析该 HTML。 They probably do something like this later:他们以后可能会做这样的事情:

someElement.innerHTML = text;

Live Example:现场示例:

 var fruits, text, fLen, i; fruits = ["Banana", "Orange", "Apple", "Mango"]; fLen = fruits.length; text = "<ul>"; for (i = 0; i < fLen; i++) { text += "<li>" + fruits[i] + "</li>"; } text += "</ul>"; document.getElementById("some-element").innerHTML = text;
 <div id="some-element"></div>

Assigning to the innerHTML property on an element makes the browser parse the HTML and creates the appropriate DOM nodes/elements, then replace the element's contents with those nodes/elements.分配给元素的innerHTML属性使浏览器解析 HTML 并创建适当的 DOM 节点/元素,然后用这些节点/元素替换元素的内容。

There are other ways, though, such as insertAdjacentHTML which lets you insert nodes/elements before an element, after it, inside it at the beginning (without replacing its existing content), or inside it at the end (without replacing its existing content).但是,还有其他方法,例如insertAdjacentHTML ,它允许您在元素之前、之后、开始时插入节点/元素(不替换其现有内容)或最后插入元素(不替换其现有内容) . For example, this inserts at the end of body :例如,这在body的末尾插入:

 var fruits, text, fLen, i; fruits = ["Banana", "Orange", "Apple", "Mango"]; fLen = fruits.length; text = "<ul>"; for (i = 0; i < fLen; i++) { text += "<li>" + fruits[i] + "</li>"; } text += "</ul>"; document.body.insertAdjacentHTML( "beforeend", text );
 <div>Content already in the `body` element before `insertAdjacentHTML`.</div>

Or you can use DOMParser if you want to parse it without adding it to any document:或者,如果您想在不将其添加到任何文档的情况下对其进行解析,则可以使用DOMParser

 var fruits, text, fLen, i; fruits = ["Banana", "Orange", "Apple", "Mango"]; fLen = fruits.length; text = "<ul>"; for (i = 0; i < fLen; i++) { text += "<li>" + fruits[i] + "</li>"; } text += "</ul>"; var parser = new DOMParser(); var doc = parser.parseFromString(text, "text/html"); var liElements = doc.querySelectorAll("li"); console.log("`li` elements found: " + liElements.length);

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

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