From 06c41ae8f0cccbe8cfe951b3ba3992c69e69ec8c Mon Sep 17 00:00:00 2001 From: anime Date: Mon, 30 Dec 2024 16:15:57 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E4=BF=AE=E5=A4=8D=E6=9F=90=E4=BA=9B?= =?UTF-8?q?=E7=8A=B6=E6=80=81=E4=B8=8B=E6=97=A0=E6=B3=95=E6=AD=A3=E5=B8=B8?= =?UTF-8?q?=E5=B7=A5=E4=BD=9C=E7=9A=84=E9=97=AE=E9=A2=98):=20=E8=BF=98?= =?UTF-8?q?=E6=98=AF=E9=80=89=E6=8B=A9span=E5=AE=8C=E5=85=A8=E5=8C=85?= =?UTF-8?q?=E8=A3=B9=EF=BC=8C=E7=AE=80=E5=8C=96=E5=A4=84=E7=90=86=E9=9A=BE?= =?UTF-8?q?=E5=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- public/js/spoilerText.js | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/public/js/spoilerText.js b/public/js/spoilerText.js index 3276636a..7a775034 100644 --- a/public/js/spoilerText.js +++ b/public/js/spoilerText.js @@ -1,6 +1,7 @@ function convertTextToSpoilerSpan(node, className, spoilerTag) { const regex = new RegExp(`${spoilerTag}(.*?)${spoilerTag}`, 'g') const wholeText = node.wholeText + let outerSpan = document.createElement('span') const fragments = [] let lastIndex = 0 let match @@ -8,7 +9,7 @@ function convertTextToSpoilerSpan(node, className, spoilerTag) { console.log('符合要求的文字' + wholeText) // 添加前面未匹配的部分 if (match.index > lastIndex) { - fragments.push( + outerSpan.appendChild( document.createTextNode(wholeText.slice(lastIndex, match.index)) ) } @@ -19,21 +20,16 @@ function convertTextToSpoilerSpan(node, className, spoilerTag) { if (className) { span.className = className } - fragments.push(span) + outerSpan.appendChild(span) // 设置lastIndex lastIndex = regex.lastIndex } - if (fragments.length) { + if (outerSpan.childNodes.length) { // 添加剩余未匹配的部分 if (lastIndex < wholeText.length) { - fragments.push(document.createTextNode(wholeText.slice(lastIndex))) + outerSpan.appendChild(document.createTextNode(wholeText.slice(lastIndex))) } - - // 替换原节点 - fragments.forEach(fragment => { - node.parentNode.appendChild(fragment) - }) - node.remove() + node.replaceWith(outerSpan) } }