简体   繁体   English

我应该使用“名称”还是“ID”作为 HTML 锚?

[英]Should I use 'Name' or 'ID' as an HTML Anchor?

I'm a little confused, I want to create a link in my HTML page that will jump to a specific section in another page.我有点困惑,我想在我的 HTML 页面中创建一个链接,该链接将跳转到另一个页面中的特定部分。 I saw examples with 'Name' as an anchor and I saw examples with 'ID'.我看到了以“名称”作为锚点的示例,也看到了带有“ID”的示例。

As I understood 'Name' is an old style and 'ID' is the new style that supported by a new browsers.据我了解,“名称”是一种旧样式,“ID”是新浏览器支持的新样式。 But what if I want to be sure that my link will work BOTH in old and new browsers?但是,如果我想确保我的链接在新旧浏览器中都能正常工作怎么办? Can I somehow combine 'Name' and 'ID' together so that my link will work in any browser no matter if it's old or new?我可以以某种方式将“名称”和“ID”组合在一起,以便我的链接可以在任何浏览器中工作,无论它是旧的还是新的?

A simple example code will be nice.一个简单的示例代码会很好。

Thanks!谢谢!

As far as I know, name attribute cannot be used as anchor reference in a URL.据我所知, name属性不能用作 URL 中的锚引用。 Also, name cannot be used on all kinds of HTML elements.此外, name不能用于所有类型的 HTML 元素。 For example, <a> elements have no valid name attribute.例如, <a>元素没有有效的name属性。

The name attribute is used for form fields and is not unique like the id attribute. name属性用于表单字段,它不像id属性那样是唯一的。 See here: Difference between id and name attributes in HTML请参阅此处: HTML 中 id 和 name 属性之间的区别

Use id attribute for for anchor, it should work as expected in any browser.使用id属性作为锚点,它应该可以在任何浏览器中按预期工作。 Have a look at this answer for implementation details: Should I make HTML Anchors with 'name' or 'id'?看看这个答案的实现细节:我应该用'name'还是'id'制作HTML Anchors?

To answer your question directly, yes, you can use both name and id to refer to named anchors .要直接回答您的问题,是的,您可以同时使用nameid来引用命名的锚点

However, there are two caveats that you must be aware of:但是,您必须注意两个警告:

  1. According to the W3C, the name attribute is obsolete .根据 W3C, name属性已过时 Even so, every modern browser will still allow it.即便如此,每个现代浏览器仍然会允许它。

  2. In the context of linking, the name attribute is tied to the a element rather than a div or span , so it must be used in that context.在链接的上下文中, name属性与a元素而不是divspan相关联,因此必须在该上下文中使用它。 If you try to use it in a div , it will not work for linking.如果您尝试在div中使用它,它将无法用于链接。 It may appear to work if you also include the id attribute, but the browser is using id to locate your link, not the name attribute.如果您还包含id属性,它可能起作用,但浏览器使用id来定位您的链接,而不是name属性。

So the following will work in all modern and ancient browsers:因此,以下内容适用于所有现代和古代浏览器:

<a name="footnote1" id="footnote1"></a>

But practically speaking, there is no longer any reason to use <a name="???"> in any document.但实际上,不再有任何理由在任何文档中使用<a name="???"> Linking with <span id="???"> is very widely supported and unless you plan on supporting 1990's era AOL browsers, there is really no point.<span id="???">的链接得到广泛的支持,除非您计划支持 1990 年代的 AOL 浏览器,否则真的没有意义。

Id is better way to do. Id 是更好的方法。 Please see the below -请看下图——

<ul class="tabs_list_block">
  <li><a href="#menu1">Menu1</a></li>
  <li><a href="#menu2">Menu2</a></li>
  <li><a href="#menu3">Menu3</a></li>
  <li><a href="#menu4">Menu4</a></li>
</ul>

<div class="tab_content">
  <div id="menu1" class="tab_content_row tab_content_row01">Menu1</div>
  <div id="menu2" class="tab_content_row tab_content_row02">Menu2</div>
  <div id="menu3" class="tab_content_row tab_content_row03">Menu3</div>
  <div id="menu4" class="tab_content_row tab_content_row04">Menu4</div>
</div>

.tabs_list_block {
    width: 100%;
    min-height: 50px; 
    display: flex;
    align-items: center;
    border-bottom: solid 1px #ccc;
    position: fixed;
    top: 0;
    width: 100%;
    background-color: #ffffff;
  } 
  .tabs_list_block li {
    border-right: solid 1px #ccc;
    flex: 1 1 auto;
    height: 100%;
    text-align: center;
  }
  .tabs_list_block li:last-child {
    border-right: 0;
  }
  .tabs_list_block li a {
    width: 100%;
    height: 100%;
    padding: 16px;
    color: #ccc;
  }
  .tab_content {
    margin-top: 50px;
  }
  .tab_content_row {
      min-height: calc(100vh - 50px);
      display: flex;
      align-items: center;
      justify-content: center;
  }
  .tab_content_row01 {
    background-color: #ffbf73;
  }
  .tab_content_row02 {
    background-color: #eaff73;
  }
  .tab_content_row03 {
    background-color: #8cf9f0;
  }
  .tab_content_row04 {
    background-color: #ddc4ff;
  }

<script type="text/javascript">
$(".tabs_list_block").find("a").click(function(e) {
    e.preventDefault();
    var section = $(this).attr("href");
    $("html, body").animate({
        scrollTop: $(section).offset().top - 0
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM