document.addEventListener('DOMContentLoaded', () => { const targetDivsSet = new Set(); // 移動先のすべてのdivを追跡するためのセット // 共通の関数:指定されたディスプレイから記事を移動 function processDisplay(selector) { const displays = document.querySelectorAll(selector); displays.forEach((display) => { const articles = display.querySelectorAll('li.article'); // 各.sc-display内のli.articleを取得 articles.forEach((article) => { const img = article.querySelector('img'); // 各article内のimg要素を取得 if (img) { const src = img.getAttribute('src'); // imgのsrc属性を取得 const match = src.match(/\/(\d{8})\.jpg$/); // 8桁の数字を抽出 if (match) { const targetId = match[1]; // 一致した数字を取得 const targetDiv = document.getElementById(targetId); // 移動先のdivを取得 if (targetDiv) { targetDiv.appendChild(article); // articleを移動 targetDivsSet.add(targetDiv); // このdivを移動先として記録 } } } }); }); } // 両方のセレクタを処理 processDisplay('.contents-wrapper .sc-display:nth-child(1)'); processDisplay('.contents-wrapper .sc-display:nth-child(2)'); processDisplay('.contents-wrapper .sc-display:nth-child(3)'); processDisplay('.contents-wrapper .sc-display:nth-child(4)'); processDisplay('.contents-wrapper .sc-display:nth-child(5)'); //
内の移動先のdivを詰める処理 const mainElement = document.querySelector('#main'); if (mainElement) { const allTargetDivs = mainElement.querySelectorAll('div[id]'); //
内のidを持つdivを取得 allTargetDivs.forEach((div) => { if (!targetDivsSet.has(div) && div.children.length === 0) { div.remove(); // 移動先に何もない場合、そのdivを削除 } }); } });