summaryrefslogtreecommitdiff
path: root/assets/js/main_new.js
diff options
context:
space:
mode:
authormayx <mayx@outlook.com>2026-05-27 02:37:23 +0000
committermayx <mayx@outlook.com>2026-05-27 02:37:23 +0000
commit156f964333ea9ca4d00d98f3c831ef05fe0b8d46 (patch)
tree1952478c0e1e58f06ba3ac7b790377cdd4b8a8a8 /assets/js/main_new.js
parent481df19596b14612a5b1bc358d9514c8d4b490bb (diff)
Update 3 filesHEADmaster
- /_data/other_repo_list.csv - /assets/js/main_new.js - /assets/js/pjax.js
Diffstat (limited to 'assets/js/main_new.js')
-rw-r--r--assets/js/main_new.js13
1 files changed, 9 insertions, 4 deletions
diff --git a/assets/js/main_new.js b/assets/js/main_new.js
index 22f2552..fc4b6ce 100644
--- a/assets/js/main_new.js
+++ b/assets/js/main_new.js
@@ -1,4 +1,8 @@
-$(function () {
+/**
+ * 根据 URL ?kw= 参数高亮页面内匹配的关键词。
+ * 提取为全局函数,供 pjax.js 在页面切换后复用,避免重复实现。
+ */
+function highlightKeyword() {
const urlParams = new URLSearchParams(window.location.search);
const keyword = urlParams.get('kw')?.trim();
@@ -10,7 +14,7 @@ $(function () {
const regex = new RegExp(`(${escapedKeyword})`, 'gi');
// 递归遍历并高亮文本节点
- const escapeHTML = str => str.replace(/[&<>"']/g,
+ const escapeHTML = str => str.replace(/[&<>"']/g,
tag => ({
'&': '&amp;',
'<': '&lt;',
@@ -41,7 +45,7 @@ $(function () {
$('section').each(function () {
highlightTextNodes(this);
});
-});
+};
function initCopyButtons() {
$('.copy').remove();
@@ -58,6 +62,7 @@ function initCopyButtons() {
});
}
-$(function() {
+$(function () {
+ highlightKeyword();
initCopyButtons();
}); \ No newline at end of file