浏览代码

【修复】重复触发点击监听问题

PJ568 2 年之前
父节点
当前提交
e9d5ef8c3b
共有 1 个文件被更改,包括 13 次插入 和 10 次删除
  1. 13 10
      index.html

+ 13 - 10
index.html

@@ -3,7 +3,7 @@
 <!-- 作者:PJ568 -->
 <!-- 项目地址:https://github.com/PJ-568/markdown.html/ -->
 <!-- 本项目遵循 CC BY-SA 4.0 International 许可协议 -->
-<!-- v 0.5.2 -->
+<!-- v 0.5.3 -->
 
 <head>
   <meta charset="utf-8">
@@ -30,6 +30,9 @@
       // Pjax 相关
       let pjax;
 
+      // 自定义 PJAX 相关
+      const existPushState = (history && history.pushState);
+
       // 初始化加载动画相关元素
       function initAni() {
         loadingBar = document.querySelector('.loading-bar');
@@ -259,24 +262,23 @@
         }
       };
 
-      // 初始化自定义 Pjax
+      // 初始化自定义 PJAX
       function initCostumedPjax() {
-        document.querySelector('#c').addEventListener('click', function (event) {
-          if (history && history.pushState) {
+        if (existPushState) {
+          document.querySelector('#c').addEventListener('click', function (event) {
             const link = event.target.closest('a[href$=".md"]');
-            if (link && link.id !== 'pjax-link') {
+            if (link) {
               try {
-                link.id = 'pjax-link';
                 event.preventDefault();
                 startLoad();
                 showMarkdown(decodePath(link.href));
                 history.pushState({ url: link.href, title: document.title, uid: link.href }, null, link.href);
               } catch (e) {
-                console.error('自定义 Pjax 出错:' + e);
+                console.error('自定义 PJAX 出错:' + e);
               }
             }
-          }
-        });
+          });
+        }
       }
 
       // 初始化内容
@@ -310,6 +312,7 @@
         initContent();	//// 初始化内容
         initTranslate();	//// 初始化页面翻译
         initPjax();	//// 初始化 Pjax
+        initCostumedPjax();	//// 初始化自定义 PJAX
         initAni();	//// 初始化加载动画
       }
 
@@ -329,6 +332,7 @@
         updatePValue();
         initContent();
         translate.execute();    // 手动触发翻译,但是理论上 translate-js 会监听页面变化
+        initCostumedPjax();	//// 初始化自定义 PJAX
         endLoad();
       })
       //// 文档内容加载完毕时触发
@@ -336,7 +340,6 @@
         refreshTranslate();
         infoTranslate();	//// 检查并提示翻译
         translate.execute();    // 手动触发翻译,但是理论上 translate-js 会监听页面变化
-        initCostumedPjax();	//// 初始化自定义 Pjax
         endLoad();
       }
     })();