Html 在 Markdown 中,链接到页面片段的最佳方式是什么,即 #some_id?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3292903/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
In Markdown, what is the best way to link to a fragment of a page, i.e. #some_id?
提问by Chris
I'm trying to figure out how to reference another area of a page with Markdown. I can get it working if I add a
我想弄清楚如何使用 Markdown 引用页面的另一个区域。如果我添加一个,我可以让它工作
<div id="mylink" />
and for the link do:
对于链接,请执行以下操作:
[My link](#mylink)
But my guess is that there's some other way to do an in-page link in Markdown that doesn't involve the straight up div
tag.
但我的猜测是,在 Markdown 中还有其他一些方法可以在不涉及直接div
标记的情况下进行页内链接。
Any ideas?
有任何想法吗?
回答by Steve Powell
See this answer.
看到这个答案。
In summary make a destination with
总之做一个目的地
<a name="sometext"></a>
inserted anywhere in your markdown markup (for example in a header:
插入 Markdown 标记中的任何位置(例如在标题中:
## heading<a name="headin"></a>
and link to it using the markdown linkage:
并使用降价链接链接到它:
[This is the link text](#headin)
or
或者
[some text](#sometext)
Don't use <div>
-- this will mess up the layout for many renderers.
不要使用<div>
——这会弄乱许多渲染器的布局。
(I have changed id=
to name=
above. See this answerfor the tedious explanation.)
(我已更改id=
为name=
上面。有关乏味的解释,请参阅此答案。)
回答by bodgix
I guess this depends on what you're using to generate html from your markdown. I noticed, that jekyll (it's used by gihub.io pages by default) automatically adds the id="" attribute to headings in the html it generates.
我想这取决于您用于从 Markdown 生成 html 的内容。我注意到,jekyll(默认情况下由 gihub.io 页面使用)会自动将 id="" 属性添加到它生成的 html 中的标题。
For example if you're markdown is
例如,如果你的降价是
My header
---------
The resulting html will look like this:
生成的 html 将如下所示:
<h2 id="my-header">My header</h2>
So you can link to it simply by [My link](#my-header)
所以你可以简单地链接到它 [My link](#my-header)
回答by Klokie
With the PHP version of Markdown, you can also link headers to fragment identifiers within the page using a syntax like either of the following, as documented here
使用Markdown的PHP 版本,您还可以使用以下任一语法将标题链接到页面内的片段标识符,如此处所述
Header 1 {#header1}
========
## Header 2 ## {#header2}
and then
进而
[Link back to header 1](#header1)
[Link back to header 2](#header2)
Unfortunately this syntax is currently only supported for headers, but at least it could be useful for building a table of contents.
不幸的是,此语法目前仅支持标题,但至少它对构建目录很有用。
回答by Nick
For anyone use Visual Studio Team Foundation Server (TFS) 2015, it really does not like embedded <a>
or <div>
elements, at least in headers. It also doesn't like emoji in headers either:
对于任何使用 Visual Studio Team Foundation Server (TFS) 2015 的人来说,它确实不喜欢嵌入<a>
或<div>
元素,至少在标题中是这样。它也不喜欢标题中的表情符号:
### Configuration
Lorem ipsum problem fixem.
Gets translated to:
被翻译成:
<h3 id="-configuration-"> Configuration </h3>
<p>Lorem ipsum problem fixem.</p>
And so links should either use that id
(which breaks thisand other preview extensions in Visual Studio), or remove the emoji:
因此链接应该使用它id
(这会破坏Visual Studio 中的这个和其他预览扩展),或者删除表情符号:
Here's [how to setup](#-configuration-) // Configuration
Here's [how to setup](#configuration) //Configuration
Where the latter version works both online in TFS andin the markdown preview of Visual Studio.
后一个版本在 TFS和Visual Studio 的降价预览中都可以在线工作。
回答by Mike
The destination anchor for a link in an HTML page may be any element with an id
attribute. See Linkson the W3C site. Here's a quote from the relevant section:
HTML 页面中链接的目标锚点可以是任何具有id
属性的元素。请参阅W3C 站点上的链接。这是相关部分的引用:
Destination anchors in HTML documents may be specified either by the A element (naming it with the name attribute), or by any other element (naming with the id attribute).
HTML 文档中的目标锚可以由 A 元素(使用 name 属性命名)或任何其他元素(使用 id 属性命名)指定。
Markdown treats HTML as HTML (see Inline HTML), so you can create your fragment identifiers from any element you like. If, for example, you want to link to a paragraph, just wrap the paragraph in a paragraph tag, and include an id:
Markdown 将 HTML 视为 HTML(请参阅内联 HTML),因此您可以从您喜欢的任何元素创建片段标识符。例如,如果您想链接到一个段落,只需将该段落包装在一个段落标记中,并包含一个 id:
<p id="mylink">Lorem ipsum dolor sit amet...</p>
Then use your standard Markdown [My link](#mylink)
to create a link to fragment anchor. This will help to keep your HTML clean, as there's no need for extra markup.
然后使用您的标准 Markdown[My link](#mylink)
创建一个链接到片段锚点。这将有助于保持您的 HTML 干净,因为不需要额外的标记。