revert revert 48f440c1b5

Bug545-huanghuiling
黄慧玲 2024-04-11 18:34:21 +08:00
parent 41e061cd2e
commit dbc1aad2d0
2 changed files with 22 additions and 2 deletions

View File

@ -9,9 +9,13 @@ function fillInWord() {
let word = getWord(); let word = getWord();
if (isRead) Reader.read(word, inputSlider.value); if (isRead) Reader.read(word, inputSlider.value);
if (!isChoose) return; if (!isChoose) return;
const element = document.getElementById("selected-words"); let element = document.getElementById("selected-words");
let index = (String)(element.value).indexOf(word);
localStorage.setItem("nowWord",element.value);
if(index === -1){
element.value = element.value + " " + word; element.value = element.value + " " + word;
} }
}
document.getElementById("text-content").addEventListener("click", fillInWord, false); document.getElementById("text-content").addEventListener("click", fillInWord, false);

View File

@ -33,6 +33,22 @@ function highLight() {
allWords = pickedWords.value + " "; allWords = pickedWords.value + " ";
} }
const list = allWords.split(" ");//将所有的生词放入一个list中用于后续处理 const list = allWords.split(" ");//将所有的生词放入一个list中用于后续处理
//判断获取的单词不能为空
if(word !== null && word !== "" && word !== " "){
let articleContent_fb2 = articleContent;
if(localStorage.getItem("nowWord").indexOf(word) !== -1){
while(articleContent_fb2.toLowerCase().indexOf(word.toLowerCase()) !== -1){
// 找到副本中和list[i]匹配的第一个单词(第一种匹配情况),并赋值给list[i]。
const index = articleContent_fb2.toLowerCase().indexOf(word.toLowerCase());
word = articleContent_fb2.substring(index, index + word.length);
articleContent_fb2 = articleContent_fb2.substring(index + word.length); // 使用副本中list[i]之后的子串替换掉副本
articleContent = articleContent.replace(new RegExp("<mark>"+word+"</mark>", "g"), word)
}
pickedWords.value = localStorage.getItem("nowWord").replace(word,"");
document.getElementById("article").innerHTML = articleContent;
return;
}
}
for (let i = 0; i < list.length; ++i) { for (let i = 0; i < list.length; ++i) {
list[i] = list[i].replace(/(^\s*)|(\s*$)/g, ""); //消除单词两边的空字符 list[i] = list[i].replace(/(^\s*)|(\s*$)/g, ""); //消除单词两边的空字符
list[i] = list[i].replace('|', ""); list[i] = list[i].replace('|', "");