简体   繁体   English

HTML 书签链接在 Wordpress 上不起作用

[英]HTML Bookmark Links not Working on Wordpress

I'm trying to create a table of content for my WordPress blog using bookmark links.我正在尝试使用书签链接为我的 WordPress 博客创建目录。 I've been reading on it and I understand there are two steps to the process First, I have to assign an id to the place I want to link to in my text eg:我一直在阅读它,并且我知道该过程有两个步骤首先,我必须为要在文本中链接到的位置分配一个 ID,例如:

<h2 id="C4">Chapter 4</h2>

And then, on the index section, I have to create the actual link, like so:然后,在索引部分,我必须创建实际的链接,如下所示:

<a href="#C4">Jump to Chapter 4</a>

I read online, however, that the foolproof way to go about it looks like this:但是,我在网上阅读到 go 的万无一失的方法如下所示:

<h2 id="C1"></h2>
<h2>Chapter 1</h2>

This is because, and I quote "You may decide not to use text because often when you jump to a spot on your site, a toolbar or something else at the top might cover up the actual beginning spot you want your visitors to see."这是因为,我引用“您可能决定不使用文本,因为通常当您跳转到站点上的某个位置时,顶部的工具栏或其他东西可能会掩盖您希望访问者看到的实际起始位置。”

I tried both ways and neither worked because once I'm trying the links out on the preview of my blog post, whenever I click on them they lead me to random places in the text.我尝试了这两种方法,但都没有奏效,因为一旦我在我的博客文章的预览中尝试链接,每当我点击它们时,它们都会将我带到文本中的随机位置。

Here are the details of my code:以下是我的代码的详细信息:

Index
<a href="#C1">What is Habit Tracking?</a>
<a href="#C2">How Does Habit Tracking Work?</a>
<a href="#C3">Which Habits Should You Track?</a>
<a href="#C4">Which Habit Tracking Tool Should You Use?</a>
<a href="#C5">A Simple Google Sheets Habit Tracker</a>
<a href="#C6">Does Habit Tracking Work?</a>
<a href="#C7">The Habit of Tracking Habits</a>

&nbsp;
<h2 id="C1"></h2>
<h2>What is Habit Tracking?</h2>
Text... 

&nbsp;
<h2 id="C2"></h2>
<h2>How Does Habit Tracking Work?</h2>
Text...

&nbsp;
<h2 id="C3"></h2>
<h2>Which Habits Should You Track?</h2>
Text....

And so on until I reach the seventh and last subtitle.依此类推,直到我到达第七个也是最后一个字幕。

Please, if anyone can help me work this out I would greatly appreciate it!请,如果有人能帮我解决这个问题,我将不胜感激!

There are two things, First: in order you to see effect, you need to have text more than your browser window height so you can see it moving, your code seems fine so it should work.有两件事,第一:为了看到效果,你需要有超过浏览器 window 高度的文本,这样你才能看到它在移动,你的代码看起来很好,所以它应该可以工作。 just add dummy content and see.只需添加虚拟内容并查看。 you can get dummy content from here https://www.lipsum.com/你可以从这里获得虚拟内容https://www.lipsum.com/

Second when browser scroll to an id it scroll to that id leaving no top margin which makes bad UX Sometimes, to fix that just add top padding to your <h2> .其次,当浏览器滚动到一个 id 时,它会滚动到该 id,没有留下顶部边距,这会导致糟糕的 UX 有时,要解决这个问题,只需在<h2>中添加顶部填充。 like喜欢

  h2{
    padding-top:100px;
   }

and it should look good.它应该看起来不错。 i created using your code here.我在这里使用您的代码创建。 https://codepen.io/jiteshdhamaiya/pen/vYGGrZg https://codepen.io/jiteshdhamaiya/pen/vYGGrZg

adjust padding accordingly.相应地调整填充。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM