diff --git a/app/static/js/word_operation.js b/app/static/js/word_operation.js index dcf38ff..7f2cdd2 100644 --- a/app/static/js/word_operation.js +++ b/app/static/js/word_operation.js @@ -102,9 +102,19 @@ function wordTemplate(word) { 不熟悉 删除 朗读 +

`; } + +/** + * 使用模板将传入的单词转换为相应的HTML字符串 +*/ +function wordTemplate(word) { + // 这个模板应当与 templates/userpage_get.html 中的

...

保持一致 + return ``; +} + /** * 删除某一词频元素 * 此处word为词频元素对应的单词 @@ -171,4 +181,50 @@ function compareWord(first, second) { return first.word < second.word ? -1 : 1; } return 0; +} + +/** + * + * + */ +function random_select_word(word) { + // 获取所有带有 "word-container" 类的

标签 + const container = document.querySelector('.word-container'); + + console.log("container",container) + + // 获取所有带有"new-word"类的

标签 + let wordContainers = container.querySelectorAll('.new-word'); + + // 检查是否存在带有"new-word"类的

标签 + if (wordContainers.length > 0) { + // 将NodeList转换为数组 + let wordContainersArray = [...wordContainers]; + + // 随机打乱数组 + for (let i = wordContainersArray.length - 1; i > 0; i--) { + const j = Math.floor(Math.random() * (i + 1)); + [wordContainersArray[i], wordContainersArray[j]] = [wordContainersArray[j], wordContainersArray[i]]; + } + + // 选取前10个元素 + let selectedWordContainers = wordContainersArray.slice(0, 10); + + // 对选取的

标签进行处理 +// selectedWordContainers.forEach(container => { +// console.log(container.outerHTML); +// }); + // 置顶 +// selectedWordContainers.forEach(p => { +// container.insertBefore(p, container.firstChild); +// }); + + wordContainersArray.forEach((p, index) => { + if (index < 10) { + p.style.display = 'block'; + } else { + p.style.display = 'none'; + } + }) + } } \ No newline at end of file diff --git a/app/templates/userpage_get.html b/app/templates/userpage_get.html index e5c8d95..6df5c02 100644 --- a/app/templates/userpage_get.html +++ b/app/templates/userpage_get.html @@ -139,11 +139,15 @@ {% if d_len > 0 %}

+ 我的生词簿 +
+ 随机选取10个 + 显示所有生词