fix: data-cn 移到 p 标签,JS 只替换文本内容
This commit is contained in:
parent
ace23ceadc
commit
973bdddbf8
@ -1,20 +1,16 @@
|
|||||||
document.addEventListener('DOMContentLoaded', function () {
|
document.addEventListener('DOMContentLoaded', function () {
|
||||||
document.querySelectorAll('blockquote[data-cn]').forEach(function (el) {
|
document.querySelectorAll('blockquote').forEach(function (el) {
|
||||||
const cn = el.getAttribute('data-cn').split('|');
|
const ps = el.querySelectorAll('p[data-cn]');
|
||||||
// 收集所有文本节点
|
if (!ps.length) return;
|
||||||
const walker = document.createTreeWalker(el, NodeFilter.SHOW_TEXT);
|
|
||||||
const nodes = [];
|
const originals = Array.from(ps).map(p => p.textContent);
|
||||||
while (walker.nextNode()) nodes.push(walker.currentNode);
|
const translations = Array.from(ps).map(p => p.getAttribute('data-cn'));
|
||||||
// 存原文
|
|
||||||
const orig = nodes.map(n => n.textContent);
|
|
||||||
let showing = 'orig';
|
let showing = 'orig';
|
||||||
|
|
||||||
function swap(to) {
|
function swap(to) {
|
||||||
if (to === 'cn') {
|
ps.forEach((p, i) => {
|
||||||
cn.forEach((text, i) => { if (nodes[i]) nodes[i].textContent = text; });
|
p.textContent = to === 'cn' ? translations[i] : originals[i];
|
||||||
} else {
|
});
|
||||||
orig.forEach((text, i) => { if (nodes[i]) nodes[i].textContent = text; });
|
|
||||||
}
|
|
||||||
showing = to;
|
showing = to;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -13,16 +13,16 @@ echo "of the human, by the human, for the human" | sed 's/human/AI/g'
|
|||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
<blockquote data-cn="重估一切价值。|— 尼采">
|
<blockquote>
|
||||||
<p>Umwertung aller Werte.</p>
|
<p data-cn="重估一切价值。">Umwertung aller Werte.</p>
|
||||||
<p>— Nietzsche</p>
|
<p data-cn="— 尼采">— Nietzsche</p>
|
||||||
</blockquote>
|
</blockquote>
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
<blockquote data-cn="无产者在这个革命中失去的只是锁链,他们获得的将是整个世界。|— 马克思">
|
<blockquote>
|
||||||
<p>Die Proletarier haben nichts in ihr zu verlieren als ihre Ketten. Sie haben eine Welt zu gewinnen.</p>
|
<p data-cn="无产者在这个革命中失去的只是锁链,他们获得的将是整个世界。">Die Proletarier haben nichts in ihr zu verlieren als ihre Ketten. Sie haben eine Welt zu gewinnen.</p>
|
||||||
<p>— Marx</p>
|
<p data-cn="— 马克思">— Marx</p>
|
||||||
</blockquote>
|
</blockquote>
|
||||||
|
|
||||||
---
|
---
|
||||||
@ -34,23 +34,23 @@ echo "of the human, by the human, for the human" | sed 's/human/AI/g'
|
|||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
<blockquote data-cn="我有一个梦想,就是今天。|— 马丁·路德·金">
|
<blockquote>
|
||||||
<p>I have a dream, today.</p>
|
<p data-cn="我有一个梦想,就是今天。">I have a dream, today.</p>
|
||||||
<p>— Martin Luther King Jr.</p>
|
<p data-cn="— 马丁·路德·金">— Martin Luther King Jr.</p>
|
||||||
</blockquote>
|
</blockquote>
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
<blockquote data-cn="民有、民治、民享。|— 林肯">
|
<blockquote>
|
||||||
<p>of the people, by the people, for the people.</p>
|
<p data-cn="民有、民治、民享。">of the people, by the people, for the people.</p>
|
||||||
<p>— Lincoln</p>
|
<p data-cn="— 林肯">— Lincoln</p>
|
||||||
</blockquote>
|
</blockquote>
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
<blockquote data-cn="直到永远的胜利。|— 切·格瓦拉">
|
<blockquote>
|
||||||
<p>Hasta la victoria siempre.</p>
|
<p data-cn="直到永远的胜利。">Hasta la victoria siempre.</p>
|
||||||
<p>— Che Guevara</p>
|
<p data-cn="— 切·格瓦拉">— Che Guevara</p>
|
||||||
</blockquote>
|
</blockquote>
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user