reference (named anchor) in markdown
有相当于下面的markdown语法:
Take me to <a href="#pookie">pookie</a>
...
<a name="pookie">this is pookie</a>
Take me to [pookie](#pookie)
should be the correct markdown syntax to jump to the anchor point named pookie.
To insert an anchor point of that name use HTML:
<a name="pookie"></a>
Markdown doesn't seem to mind where you put the anchor point. A useful place to put it is in a header. For example:
### <a name="tith"></a>This is the Heading
works very well. (I'd demonstrate here but SO's renderer strips out the anchor.)
Note on self-closing tags and id=
versus name=
An earlier version of this post suggested using <a id='tith' />
, using the self-closing syntax for XHTML, and using the id
attribute instead of name
.
XHTML allows for any tag to be 'empty' and 'self-closed'. That is, <tag />
is short-hand for <tag></tag>
, a matched pair of tags with an empty body. Most browsers will accept XHTML, but some do not. To avoid cross-browser problems, close the tag explicitly using <tag></tag>
, as recommended above.
Finally, the attribute name=
was deprecated in XHTML, so I originally used id=
, which everyone recognises. However, HTML5 now creates a global variable in JavaScript when using id=
, and this may not necessarily be what you want. So, using name=
is now likely to be more friendly.
(Thanks to Slipp Douglas for explaining XHTML to me, and nailer for pointing out the HTML5 side-effect — see the comments and nailer 's answer for more detail. name=
appears to work everywhere, though it is deprecated in XHTML.)
Use a name
. Using an id
isn't necessary in HTML 5 and will create global variables in your JavaScript
See the HTML 5 specification, 5.9.8 Navigating to a fragment identifier - both id
and name
are used.
It's important to know that most browsers still turn IDs into global variables. Here's a quick test. Using a name
avoids creating globals and any conflicts that may result.
Example using a name:
Take me to [pookie](#pookie)
And the destination anchor:
### <a name="pookie"></a>Some heading
On bitbucket.org the voted solution wouldn't work. Instead, when using headers (with ##), it is possible to reference them as anchors by prefixing them as #markdown-header-my-header-name, where #markdown-header- is an implicit prefix generated by the renderer, and the rest is the lower-cased header title with dashes replacing spaces.
Example
## My paragraph title
will produce an implicit anchor like this
#markdown-header-my-paragraph-title
The whole URL before each anchor reference is optional, ie
[Some text](#markdown-header-my-paragraph-title)
is equivalent of
[Some text](https://bitbucket.org/some_project/some_page#markdown-header-my-paragraph-title)
provided that they are in the same page.
Source: https://bitbucket.org/tutorials/markdowndemo/overview (edit source of this .md file and look at how anchors are made).
链接地址: http://www.djcxy.com/p/89008.html上一篇: 在Firefox中的地址栏中命名为锚点
下一篇: 参考(命名锚点)在降价