fix: 原地切换文本,林肯补中文

This commit is contained in:
小橘 2026-04-16 04:31:40 +00:00
parent 45014e8ac9
commit df02a5bb70
2 changed files with 17 additions and 8 deletions

View File

@ -1,13 +1,22 @@
document.addEventListener('DOMContentLoaded', function () { document.addEventListener('DOMContentLoaded', function () {
document.querySelectorAll('blockquote[data-cn]').forEach(function (el) { document.querySelectorAll('blockquote[data-cn]').forEach(function (el) {
const cn = el.getAttribute('data-cn'); const cn = el.getAttribute('data-cn');
const tip = document.createElement('div'); // 找第一个文本节点(引言正文),存原文
tip.className = 'quote-cn'; const textNode = Array.from(el.childNodes).find(n => n.nodeType === Node.TEXT_NODE && n.textContent.trim());
tip.textContent = cn; if (!textNode) return;
el.appendChild(tip); const original = textNode.textContent;
let showing = 'orig';
el.addEventListener('mouseenter', () => tip.classList.add('show')); function swap(to) {
el.addEventListener('mouseleave', () => tip.classList.remove('show')); textNode.textContent = to === 'cn' ? cn + '\n' : original;
el.addEventListener('click', () => tip.classList.toggle('show')); showing = to;
}
el.addEventListener('mouseenter', () => swap('cn'));
el.addEventListener('mouseleave', () => swap('orig'));
el.addEventListener('click', (e) => {
e.preventDefault();
swap(showing === 'orig' ? 'cn' : 'orig');
});
}); });
}); });

View File

@ -45,7 +45,7 @@ I have a dream, today.
--- ---
<blockquote> <blockquote data-cn="民有、民治、民享。">
of the people, by the people, for the people. of the people, by the people, for the people.
— 林肯 — 林肯