3 回答
TA贡献1784条经验 获得超2个赞
let html = ''
for(let i = 0;i < data.length;i++) {
let div = `<div>${data[i].title}</div>`
html += i % 2 === 0 ? `<li>` + div : div + `</li>`
}
if (data.length % 2 === 1) {
html += `</li>`
}
html = `<ul>${html}</ul>
TA贡献1893条经验 获得超10个赞
var html = ''
for(let i=0;i<a.data.length;i++){
var _html = ''
for(let o in a.data[i]){
_html+=`<div>${a.data[i][o]}</div>`
}
html+='<li>'+_html+'</li>'
}
document.getElementById("app").innerHTML = '<ul>'+html+'</ul>'
TA贡献1827条经验 获得超9个赞
let fragment = document.createDocumentFragment();
let ulNode = document.createElement("ul");
let liNodes = []
data.map((item, index) => {
let liNode = document.createElement("li");
liNode.innerHTML = item.title
liNodes.push(liNode)
if (index % 2 !== 0) {
let divNode = document.createElement("div");
console.log(liNodes)
divNode.appendChild(liNodes[index - 1])
divNode.appendChild(liNodes[index])
ulNode.appendChild(divNode)
}
})
const dataLength = data.length;
if (dataLength % 2 !== 0) {
let divNode = document.createElement("div");
divNode.appendChild(liNodes[dataLength - 1])
ulNode.appendChild(divNode)
}
fragment.appendChild(ulNode)
document.getElementById("app").appendChild(fragment)
添加回答
举报