HTML 段落片段 ID

HTML Paragraph Fragment Id

如何http://arstechnica.com/science/2015/01/thin-atmosphere-is-enough-to-keep-many-exoplanets-spinning/#p2 link到相应页面的第二段?我知道它与“#p2”片段有关,但有问题的段落没有 ID "p2"。

在 Windows 7

上使用 Firefox 31.0

这是在 <article> 下面,第一个 <p><p class="byline" itemprop="author creator",第二个 <p> 是您所指的那个到。搜索第一个<p>,你就会得到答案。 所以 #p2 指的是第二个 <p>

这当然是自定义 javascript 行为。如果您好奇,请查看文件 ars.min.ce8deeda61d4ec728127c4f0c17cf83e.js。它被最小化了,你可以美化它并找到看起来像这样的 Window.onhashchange 事件处理程序:

ars.setup_hashchange = function() {
    var a = function() {
    hash = window.location.hash.replace(/^#/, "");
    if (!hash)
        return;
    var a = $("#" + hash + ", *[name=" + hash + "]"), b = hash.match(/^p([0-9]+)(n)?$/), c = hash.match(/^h([0-9]+)$/);
    if (a.length)
        ars.scroll_to(a.first());
    else if (b) {
        var d = $(".article-content > p")[Math.max(0, b[1] - 1)];
        b[2] && (d = $(d).next()), d && ars.scroll_to($(d))
    } else if (c) {
        var e = $(".article-content").find("> h2, > h3, > h4").filter(":not([data-no-jump])")[Math.max(0, c[1] - 1)];
        e && ars.scroll_to($(e))
    }
};
$(window).on("hashchange", a), setTimeout(a, 0)

所以它从 location.hash 中获取段落索引,然后在 $(".article-content > p") 集合中找到相应的 p 元素,最后将文档滚动到它。