data
从javascript,我必须排序后,点击它。这是我的密码:
const data = [
{
"id": 2,
"title": "ASD",
"src": "https://cloudfour.com/examples/img-currentsrc/images/kitten-small.png"
},
{
"id": 1,
"title": "FGH",
"src": "https://cloudfour.com/examples/img-currentsrc/images/kitten-small.png"
}
]
const info_container = document.querySelector('.info-container')
const sort = () => {
data.sort((a, b) => parseFloat(a.id) - parseFloat(b.id))
fetchData()
}
const fetchData = () => {
data.forEach((item, index) => {
const img = document.createElement("img");
const title = document.createElement('h3')
const node = document.createTextNode(item.src);
const node_title = document.createTextNode(item.title)
title.appendChild(node_title)
img.src = item.src
info_container.appendChild(title)
info_container.appendChild(img);
})
}
window.onload = function() {
fetchData()
}
<div><button onclick="sort()">sort</button></div>
<div class="info-container">
</div>
http://plnkr.co/edit/Yjn4jaLN45pYJHFZBZLn?p=preview
不是更新我的实际视图,而是创建一个新的视图,如何修复?
提前谢谢你的回答!