Disqus「显示评论」按钮失效问题求助
解决Disqus隐藏评论按钮点击无响应问题
你的问题核心是:只加载了Disqus的评论计数脚本,但没有给"Show Comments"按钮绑定加载评论的触发逻辑。无评论页面能正常加载是因为没有计数干扰,而有评论的页面中count.js可能修改了按钮的默认行为,导致点击没反应。
以下是修复方案,直接替换你的现有代码:
修改后的HTML结构
<noscript>Please enable JavaScript to view the comments.</noscript> <section class="post-comments"> <a id="show-disqus" class="post-comments-activate" data-disqus-identifier="/m30-linux.html">Show Comments</a> <div id="disqus_thread"></div> </section>
整合后的脚本代码
// 评论计数脚本 var disqus_shortname = 'freshlex'; (function () { var s = document.createElement('script'); s.async = true; s.type = 'text/javascript'; s.src = '//' + disqus_shortname + '.disqus.com/count.js'; (document.getElementsByTagName('HEAD')[0] || document.getElementsByTagName('BODY')[0]).appendChild(s); }()); // 评论加载触发逻辑 document.getElementById('show-disqus').addEventListener('click', function(e) { e.preventDefault(); // 隐藏按钮,避免重复点击 this.style.display = 'none'; // 初始化Disqus评论 var disqus_config = function () { this.page.url = 'https://john.soban.ski/m30-linux.html'; this.page.identifier = '/m30-linux.html'; }; (function() { var d = document, s = d.createElement('script'); s.src = '//' + disqus_shortname + '.disqus.com/embed.js'; s.setAttribute('data-timestamp', +new Date()); (d.head || d.body).appendChild(s); })(); });
关键修改说明
- 移除了全局提前声明的Disqus变量,改为在点击事件内动态初始化,避免和计数脚本冲突
- 给按钮添加点击事件监听,点击后自动隐藏按钮并加载Disqus嵌入脚本
- 统一在
disqus_config里配置页面URL和标识符,符合Disqus官方规范 - 确保脚本异步加载,不影响页面其他内容
这样修改后,无论页面有没有评论,点击按钮都能正常加载Disqus评论区。
内容的提问来源于stack exchange,提问作者John Sobanski
相关产品推荐
相关产品推荐

