【发布时间】:2021-07-17 11:50:48
【问题描述】:
const liArray = [5.6, 8.7, 1.3, 10, 56];
const parentElement = document.getElementById("myDiv");
liArray.forEach((currentValue, index) => {
var elm = document.createElement('p');
elm.innerHTML = `${index + 1}. ${currentValue}`;
parentElement.appendChild(elm);
});
<div id="myDiv"></div>
我有这段代码为数组中的每个项目创建一个新的段落元素,因此结果将是:
1. 5.6
2. 8.7
3. 1.3
4. 10
5. 56
但我想在创建元素时自动将圆括号() 添加到最小和最大数字,这样创建的元素就会像这样
1. 5.6
2. 8.7
3. (1.3)
4. 10
5. (56)
我该怎么做?
【问题讨论】:
标签: javascript html jquery dom frontend