fix: 原地切换文本,林肯补中文
This commit is contained in:
parent
45014e8ac9
commit
df02a5bb70
@ -1,13 +1,22 @@
|
||||
document.addEventListener('DOMContentLoaded', function () {
|
||||
document.querySelectorAll('blockquote[data-cn]').forEach(function (el) {
|
||||
const cn = el.getAttribute('data-cn');
|
||||
const tip = document.createElement('div');
|
||||
tip.className = 'quote-cn';
|
||||
tip.textContent = cn;
|
||||
el.appendChild(tip);
|
||||
// 找第一个文本节点(引言正文),存原文
|
||||
const textNode = Array.from(el.childNodes).find(n => n.nodeType === Node.TEXT_NODE && n.textContent.trim());
|
||||
if (!textNode) return;
|
||||
const original = textNode.textContent;
|
||||
let showing = 'orig';
|
||||
|
||||
el.addEventListener('mouseenter', () => tip.classList.add('show'));
|
||||
el.addEventListener('mouseleave', () => tip.classList.remove('show'));
|
||||
el.addEventListener('click', () => tip.classList.toggle('show'));
|
||||
function swap(to) {
|
||||
textNode.textContent = to === 'cn' ? cn + '\n' : original;
|
||||
showing = to;
|
||||
}
|
||||
|
||||
el.addEventListener('mouseenter', () => swap('cn'));
|
||||
el.addEventListener('mouseleave', () => swap('orig'));
|
||||
el.addEventListener('click', (e) => {
|
||||
e.preventDefault();
|
||||
swap(showing === 'orig' ? 'cn' : 'orig');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@ -45,7 +45,7 @@ I have a dream, today.
|
||||
|
||||
---
|
||||
|
||||
<blockquote>
|
||||
<blockquote data-cn="民有、民治、民享。">
|
||||
of the people, by the people, for the people.
|
||||
|
||||
— 林肯
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user