跳转到内容

模板:Collapse/common.js

来自SCR公司
By H留言 | 贡献2026年1月7日 (三) 02:22的版本
(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)
document.addEventListener("DOMContentLoaded", () => {
 const headers = document.querySelectorAll('.scr-collapse-header');
 headers.forEach(header => {
   const content = header.nextElementSibling;
   const symbol = header.querySelector('.scr-collapse-symbol');
   const textOpen = header.getAttribute('data-text-open') || '+ 展开';
   const textClose = header.getAttribute('data-text-close') || '- 收起';
   const defaultState = header.getAttribute('data-default') || 'closed';
   if (defaultState === 'open') {
     content.style.maxHeight = content.scrollHeight + "px";
     symbol.textContent = textClose;
   } else {
     content.style.maxHeight = "0";
     symbol.textContent = textOpen;
   }
 header.addEventListener('click', () => {
     if (content.style.maxHeight && content.style.maxHeight !== "0px") {
       content.style.maxHeight = "0";
       symbol.textContent = textOpen;
     } else {
       content.style.maxHeight = content.scrollHeight + "px";
       symbol.textContent = textClose;
     }
   });
 });
});