-1

I am using Tocify to make a TOC and I'm having some trouble. In this TOC I have: "chapter 1", "chapter 2"...

In my site there are some places when I say "See chapter 1". I have to include "a href" to "chapter 1" in my "See chapter 1" html text. But I don't know what I have to write in that "a href=#....".

How can I do that?

Mosh Feu
  • 28,354
  • 16
  • 88
  • 135
mbc
  • 3
  • 1

1 Answers1

1

It is done as follows:

Basically you need to set an ID to a element you want to reference. Then you can write:

<a href="#id">See chapter ID</a>

<h2 id="chapter1">Chapter 1</h2>
<p>Your paragraphs</p>
<p> ... </p>
<a href="#chapter2">See chapter 2</a>

<h2 id="chapter2">Chapter 2</h2>
<p>Your paragraphs</p>
<p> ... </p>
<a href="#chapter1">See chapter 1</a>

Hope this helps you.

EDIT:

Try something like this:

$(document).ready(function() {

  var link = $('#linkId');

  var chapter = $('#chapterId');
  var position = chapter.position(); // according to jQuery api
  var offsetTop = position.top; // this should be the absolute offset of the page's top

  // Calls the tocify method on your HTML elements.
  link.tocify();
  chapter.tocify();

  //Call the tocify method with options
  link.tocify({
    showEffect: "fadeIn"
    scrollTo: offsetTop, 
    smoothScroll: false
  });
  • You cannot use tocify and #-href's at the same time because as you saw they override the url. So you have to use a jQuery or tocify method (see api's) to scroll to the specific element.

  • Or you may don't want to use tocify on links.

F. Müller
  • 3,969
  • 8
  • 38
  • 49
  • Thank you. It's a great solution. But it changes the #url that it's generated through Tocify when TOC is created. It would be possible to get that tocify's #? – mbc Mar 06 '13 at 10:50
  • I can somehow understand what you mean but you may show me the code that you use? – F. Müller Mar 06 '13 at 11:13
  • With Tocify I get http://host/mysite/main.html#Chapter2:TheTitle when I click on the "Chapter 2: The Title" in my TOC (generated with Tocify). That #Chapter2:TheTitle is automatically generated with Tocify. If I write

    Chapter 2

    ....See chapter 2 (it works) when I click on "See chapter 2" then I will get http://host/mysite/main.html#chapter2. I would like to know if it's possible to use/get the same # that is generated by Tocify.
    – mbc Mar 06 '13 at 11:31