模板:Collapse/common.js
外观
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;
}
});
});
});