【发布时间】:2019-01-18 11:07:03
【问题描述】:
当使用 Disqus 锚链接(例如 url-to/my-blog-post#disqus_thread)访问我的博客 URL 时,浏览器不会向下滚动到 @ 987654322@元素。
相反,如果我已经在 url-to/my-blog-post 页面上并点击指向 url-to/ 的链接my-blog-post#disqus_thread它向下滚动到div。
我认为这是加载问题。浏览器访问该页面并没有找到 div 元素,因为它仍然是由 Disqus JS 生成的。
我试图改变 JS 代码的位置,但没有任何改变。
这是我博客中的示例帖子网址: https://2bluebuffalo-en.blogspot.com/2018/11/how-to-crop-square-video-macos.html#disqus_thread 它不起作用,但如果您点击“By Blue Buffalo”旁边的链接(在分享按钮上方,现在写着“2 条评论”),锚链接就会起作用。
解决方法
好的,这就是我找到的解决方法。它工作得很好。以防万一有人点击该链接。
<script>
setTimeout(function(){
var hash = window.location.hash.substr(1);
if (hash == "disqus_thread") {
location.hash = "#comments";
location.hash = "#" + hash;
};
}, 1000);
</script>
等待是因为它需要我一秒钟。加载所有内容(有时甚至更多)。可以改的。
【问题讨论】:
-
改用#cmets怎么样?
-
嗨@IanGilroy 谢谢。它使用#cmets 工作。问题是我没有在链接末尾自动添加#disqus_thread 的Disqus JS 文件。所以我无法将链接 href 更改为 path/to/post#cmets。
-
@Umberto 你可以编辑博客的 HTML 来添加 disqus 脚本!
-
谢谢@Gourav 我试过了,但实际上似乎只有当用户关闭时才加载 Disqus 插件。或者至少没有真正改变。所以我更喜欢维护原始配置并在上面添加该脚本。你也在 Blogger 上使用 Disqus 吗?
标签: javascript html anchor blogger disqus