当我添加外部JavaScript文件时,“grid”函数不再显示为grid。但是当我删除JS时,grid函数又回来了。为什么?
我试图删除JavaScript。
document.addEventListener('DOMContentLoaded', function() {
// Select all div elements with the class 'time-div'
const divs = document.querySelectorAll('.container');
// Convert the NodeList to an array for easier manipulation
const divArray = Array.from(divs);
// Sort the array based on the data-time attribute
divArray.sort(function(a, b) {
const timeA = new Date(a.getAttribute('data-time'));
const timeB = new Date(b.getAttribute('data-time'));
return timeA - timeB;
});
// Clear the existing order of divs
divs.forEach(div => div.remove());
// Append the divs in the new order
divArray.forEach(div => document.body.appendChild(div));
});
个字符
1条答案
按热度按时间1hdlvixo1#
您的代码将排序后的元素附加到
<body>
,而不是“网格”<div>
。获取网格容器的引用并将元素添加到其中:字符串