Visual Studio JavaScript正在破坏我的“网格”功能?

ikfrs5lh  于 2023-11-21  发布在  Java
关注(0)|答案(1)|浏览(124)

当我添加外部JavaScript文件时,“grid”函数不再显示为grid。但是当我删除JS时,grid函数又回来了。为什么?
我试图删除JavaScript。

  1. document.addEventListener('DOMContentLoaded', function() {
  2. // Select all div elements with the class 'time-div'
  3. const divs = document.querySelectorAll('.container');
  4. // Convert the NodeList to an array for easier manipulation
  5. const divArray = Array.from(divs);
  6. // Sort the array based on the data-time attribute
  7. divArray.sort(function(a, b) {
  8. const timeA = new Date(a.getAttribute('data-time'));
  9. const timeB = new Date(b.getAttribute('data-time'));
  10. return timeA - timeB;
  11. });
  12. // Clear the existing order of divs
  13. divs.forEach(div => div.remove());
  14. // Append the divs in the new order
  15. divArray.forEach(div => document.body.appendChild(div));
  16. });

个字符

1hdlvixo

1hdlvixo1#

您的代码将排序后的元素附加到<body>,而不是“网格”<div>。获取网格容器的引用并将元素添加到其中:

  1. // Append the divs in the new order
  2. const grid = document.querySelector(".grid");
  3. divArray.forEach(div => grid.appendChild(div));

字符串

相关问题