visual-studio <a> 锚标记的“名称”属性是否已过时?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/608222/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 10:14:22  来源:igfitidea点击:

Is the 'name' attribute considered outdated for <a> anchor tags?

htmlvisual-studiotagsxhtml

提问by Zack Peterson

Visual Studio doesn't like on-page anchor tags:

Visual Studio 不喜欢页面上的锚标记:

Validation (XHTML 1.0 Transitional): Attribute 'name' is considered outdated. A newer construct is recommended.

验证(XHTML 1.0 Transitional):属性“name”被认为是过时的。建议使用较新的构造。

I'm using nameattributes in this way…

我正在name以这种方式使用属性......

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation="http://www.w3.org/MarkUp/SCHEMA/xhtml11.xsd" xml:lang="en">
    ...
    <body>
        ...
        <p>On this page&hellip;</p>
        <ul>
            <li><a href="#one">Section One</a></li>
            ...
        </ul>
        ...
        <h2><a name="one">Section One</a></h2>
        ...
    </body>
</html>

Is there really a more-modern way of doing this? Or is Visual Studio full of crap?

真的有更现代的方法吗?还是 Visual Studio 充满了废话?

回答by phihag

You should use the idattribute instead. Works the same way, and you don't need an artifical <a name=...>, but simply

您应该改用该id属性。工作方式相同,您不需要人工<a name=...>,而只是

<h2 id="one">Section One</h2>

回答by Paul Dixon

name attributes are deprecated in XHTML 1.0- you can use an id attribute in the same way though, see Fragment Identifiersin the HTML Compatibility Guidelinesof the XHTML spec.

name 属性在XHTML 1.0中已被弃用- 您可以以相同的方式使用 id 属性,请参阅XHTML 规范的HTML 兼容性指南中的片段标识符

So you can simply use

所以你可以简单地使用

<h2><a id="one">Section One</a></h2>

But note that the 1.0 spec recommends playing it safe with something like this:

但请注意,1.0 规范建议安全地使用以下内容:

<h2><a name="one" id="one">Section One</a></h2>

However, your fragment uses XHTML 1.1, where the name attribute has been entirely removedfrom aand mapelements - so you can only use an id.

但是,您的片段使用XHTML 1.1,其中 name 属性已完全aandmap元素中删除- 因此您只能使用 id。

回答by sblundy

I believe the modern approach is to use the idattribute, which would be evaluated as an anchor. For example, if you changed

我相信现代方法是使用id属性,它将被评估为锚点。例如,如果你改变了

<h2><a name="one">Section One</a></h2>

to

<h2><a id="one">Section One</a></h2>

You would still address it as page.html#one.

您仍然会将其称为page.html#one.

回答by jfrobishow

You can also link on a section header :

您还可以链接部分标题:

Table of Contents

目录

<P>
    <A href="#section1">Introduction</A><BR>
    <A href="#section2">Some background</A><BR>
    <A href="#section2.1">On a more personal note</A><BR>
    ...the rest of the table of contents...
    ...the document body...

    <H2 id="section1">Introduction</H2>
    ...section 1...

    <H2 id="section2">Some background</H2>
    ...section 2...

    <H3 id="section2.1">On a more personal note</H3>
    ...section 2.1...

[...]
</P>

Source: http://www.w3.org/TR/REC-html40/struct/links.html

来源:http: //www.w3.org/TR/REC-html40/struct/links.html

回答by James Curran

name= attributes are for labeling elements in a form, and can only be used on <form> elements (input, textarea, select etc). For everything else, ID= is used. Exactly why the W3C folks thought two different ways of naming an element (with different sets of allowable characters) were needed is not readily known.

name= 属性用于标记表单中的元素,只能用于 <form> 元素(输入、文本区域、选择等)。对于其他所有内容,使用 ID=。W3C 人员认为需要两种不同的元素命名方式(具有不同的允许字符集)的确切原因尚不清楚。

回答by waanders

But here http://www.w3.org/TR/html4/struct/links.html#h-12.2.3I read this: "Some older user agents don't support anchors created with the id attribute." So?

但在这里http://www.w3.org/TR/html4/struct/links.html#h-12.2.3我读到:“一些较旧的用户代理不支持使用 id 属性创建的锚点。” 所以?

回答by OverloadUT

I believe the proper way to do it is <a id="one">

我相信正确的做法是 <a id="one">

回答by rogeriopvl

Yes it is outdated. You should replace with the "id" attribute.

是的,它已经过时了。您应该替换为“id”属性。

Quoting w3schools page:

引用 w3schools 页面:

"The id Attribute Replaces The name Attribute HTML 4.01 defines a name attribute for the elements a, applet, frame, iframe, img, and map. In XHTML the name attribute is deprecated. Use id instead."

" id 属性替换 name 属性 HTML 4.01 为元素 a、applet、frame、iframe、img 和 map 定义了 name 属性。在 XHTML 中,name 属性已被弃用。请改用 id。"

http://www.w3schools.com/Xhtml/xhtml_syntax.asp

http://www.w3schools.com/Xhtml/xhtml_syntax.asp

回答by hotshot309

Until <a name="..."></a>is no longer supported by the (X)HTML standard you are using--and not just deprecated--it may be safest to use both nameand idon anchors linking to a part of the same page. From the W3C's XHTML 1 spec:

<a name="..."></a>您正在使用的 (X)HTML 标准不再支持直到不再支持 - 不仅不推荐使用 -在链接到同一页面一部分的锚点上同时使用name和可能是最安全的id。来自W3C 的 XHTML 1 规范

In XML, URI-references RFC2396that end with fragment identifiers of the form "#foo"do not refer to elements with an attribute name="foo"; rather, they refer to elements with an attribute defined to be of type ID, e.g., the idattribute in HTML 4. Many existing HTML clients don't support the use of ID-type attributes in this way, so identical values may be supplied for both of these attributes to ensure maximum forward and backward compatibility (e.g., <a id="foo" name="foo">...</a>).

在 XML 中,以表单的片段标识符结尾的URI 引用RFC2396"#foo"不引用具有属性的元素name="foo";相反,它们指的是具有定义为类型 ID 的属性的元素,例如,idHTML 4 中的属性。许多现有的 HTML 客户端不支持以这种方式使用 ID 类型的属性,因此可能会提供相同的值这两个属性都可以确保最大的向前和向后兼容性(例如,<a id="foo" name="foo">...</a>).