Is the 'name' attribute considered outdate

2019-02-08 09:09发布

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

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

I'm using name attributes in this way…

<?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?

9条回答
迷人小祖宗
2楼-- · 2019-02-08 09:48

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

Quoting w3schools page:

"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."

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

查看更多
在下西门庆
3楼-- · 2019-02-08 09:49

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

查看更多
家丑人穷心不美
4楼-- · 2019-02-08 09:54

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 name and id on anchors linking to a part of the same page. From the W3C's XHTML 1 spec:

In XML, URI-references RFC2396 that 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 id attribute 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>).

查看更多
叼着烟拽天下
5楼-- · 2019-02-08 10:00

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

查看更多
疯言疯语
6楼-- · 2019-02-08 10:01

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

<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.

查看更多
ら.Afraid
7楼-- · 2019-02-08 10:02

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.

查看更多
登录 后发表回答