【问题标题】:Link to an element within the current page [closed]链接到当前页面中的元素[关闭]
【发布时间】:2012-09-05 08:34:49
【问题描述】:

我创建了一个 HTML 页面,其中包含许多带有如下标题的表格:内容、Main_Page、文档、费用等。

我想为页面顶部创建一个链接。当我单击该链接时,它应该转到特定部分。所以我使用下面的代码来映​​射内容。但这对我不起作用。

<a href="#Content">Content Section</a>

【问题讨论】:

  • 您的区域是否标有&lt;a name="Content"&gt;&lt;/a&gt;
  • 我不明白这不是一个真正的问题。对我来说,这个问题和答案很有意义。
  • @MaxGoodridge - 我同意,这显然是一个真正的问题。即使是标题也很适合这个问题。也许关闭者可以建议进行编辑,以便他们乐于将其称为“真正的问题”。这对他们来说应该很容易,因为如果这不是一个“真正的问题”,它真的足够接近。好的,让我们取消关闭它。
  • @KevinFegan 谢谢 - 任何人都可以随时提交取消关闭所需的更改!

标签: html


【解决方案1】:

给你想要“跳转”的元素一个明确的 ID,像这样:

<p id="idOfTag">Your content goes here</p>

然后在页面顶部的链接中,使其引用该元素的 id(注意#):

<a href="#idOfTag">Jump</a>

包含多个链接的完整示例:

<ul>
  <li><a href="#contentParagraph">Content</a></li>
  <li><a href="#mainPageParagraph">Main page</a></li>
  <li><a href="#documentParagraph">Document</a></li>
  <li><a href="#expensesParagraph">Expenses</a></li>
</ul>
<p id="contentParagraph">
  <h4>Content</h4>
  Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor
  in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</p>
<p id="mainPageParagraph">
  <h4>Main page</h4>
  Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor
  in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</p>
<p id="documentParagraph">
  <h4>Document</h4>
  Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor
  in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</p>
<p id="expensesParagraph">
  <h4>Expenses</h4>
  Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor
  in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</p>

【讨论】:

  • 我打算在我给出答案后这样做,但是当我发布它时,问题已经被编辑了。但我会记住你的建议(也编辑了我的评论)..
  • 这个答案比选的好
  • @RichJ 谢谢!再次看到答案,我添加了一些解释和代码sn-p以提高质量:)
【解决方案2】:

看起来问题已得到解答,但如果您想在过渡到这些元素时使用滚动效果,请使用一些 JS 片段。 $(函数() {

    function filterPath(string) {
        return string
        .replace(/^\//,'')
        .replace(/(index|default).[a-zA-Z]{3,4}$/,'')
        .replace(/\/$/,'');
    }

    var locationPath = filterPath(location.pathname);
    var scrollElem = scrollableElement('html', 'body');

    // Any links with hash tags in them (can't do ^= because of fully qualified URL potential)
    $('a[href*=#]').each(function() {

        // Ensure it's a same-page link
        var thisPath = filterPath(this.pathname) || locationPath;
        if (  locationPath == thisPath
            && (location.hostname == this.hostname || !this.hostname)
            && this.hash.replace(/#/,'') ) {

                // Ensure target exists
                var $target = $(this.hash), target = this.hash;
                if (target) {

                    // Find location of target
                    var targetOffset = $target.offset().top;
                    $(this).click(function(event) {

                        // Prevent jump-down
                        event.preventDefault();

                        // Animate to target
                        $(scrollElem).animate({scrollTop: targetOffset}, 2000, function() {

                            // Set hash in URL after animation successful
                            location.hash = target;

                        });
                    });
                }
        }

    });

    // Use the first element that is "scrollable"  (cross-browser fix?)
    function scrollableElement(els) {
        for (var i = 0, argLength = arguments.length; i <argLength; i++) {
            var el = arguments[i],
            $scrollElement = $(el);
            if ($scrollElement.scrollTop()> 0) {
                return el;
            } else {
                $scrollElement.scrollTop(1);
                var isScrollable = $scrollElement.scrollTop()> 0;
                $scrollElement.scrollTop(0);
                if (isScrollable) {
                    return el;
                }
            }
        }
        return [];
    }

});

【讨论】:

    【解决方案3】:

    您可以为您的anchor 标签使用name 属性来实现此目的。

    假设您有一个div,ID 为content

    <div id="content">This is my div</div>
    

    接下来确保你有一个anchor标签,其name属性与divcontentid相同

    <a href="#" name="content">Click to go to the top</a>
    

    Live Demo.

    向下滚动查看链接

    另一种方法是

    <div id="content">My div</div>
    

    那么你的锚标签的href应该是#content

    <a href="#content">Click to go to top</a>
    

    Live Demo.

    【讨论】:

      【解决方案4】:

      您需要为链接创建一个锚点。这样做的现代方法是给适当的元素一个id="Content" 属性。较旧的方法是使用&lt;a name="Content"&gt;&lt;/a&gt;

      【讨论】:

      • 一个好问题的好答案......即使有些人认为这不是一个“真正的问题”(我不同意)并且已经结束了这个问题。我无法想象为什么,除了一些人(错误地)认为如果可以在 Google 上找到某些东西,那么它不值得在 SE/SO 上被问到。
      • 是否可以链接到基于id 以外的其他属性的元素?我尝试过在线搜索并使用 CSS 选择器,但没有奏效。
      • @TigerYu 有一个建议可以链接到特定的文本,以便搜索引擎可以将您链接到页面上靠近搜索结果的点。见github.com/WICG/scroll-to-text-fragment#readme
      • 有人对name="fancy"name="#fancy"之间的区别有什么想法吗?
      • @CeylanMumunKocabaş 你可以使用name="#fancy",但你的链接必须以##fancy结尾。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多