2013-06-07 31 views
26

The HTML code <a name="some_bookmark">text</a> is very useful for creating links to specific sections of a page (e.g., page.html#some_bookmark). However, the W3C spec now marks the name attribute of the a tag as "obsolete."Se <a name="..."> is obsolete, what is preferred?

If this is the case, then what is preferred? Is there a new <bookmark> tag or similar?

+0

Questa domanda è già stata posta qui: https://webmasters.stackexchange.com/q/12798/10884 – chharvey

risposta

21

You can place an id="" attribute on any element and it will have the same effect.

These are typically placed on heading elements.

+8

nome e l'ID non sono intercambiabili . id deve essere univoco per l'intera pagina e il nome può essere ripetuto. –

2

you should use id="" attribute. :)

10

Have a look at the HTML5 spec.

At Obsolete features troverete:

Gli autori non dovrebbero specificare l'attributo name su a elementi.

Cliccando sul name, troverete:

[I seguenti attributi sono obsoleti (anche se gli elementi sono ancora parte del linguaggio), e non deve essere utilizzato da autori:]

  • name su a elementi (ad eccezione di quanto indicato nella sezione precedente)
  • name su embed elementi
  • name su img elementi
  • name su option elementi

→ utilizzare l'attributo id invece.

Fare clic su id. Vedrai che id è un global attribute, questo significa che può essere utilizzato su qualsiasi elemento.

L'attributo id specifica l'identificatore univoco (ID) dell'elemento.
[...]
Nota: identificatore univoco di un elemento può essere utilizzato per una varietà di scopi, soprattutto come un modo per collegare parti specifiche di un documento utilizzando identificatori di frammento, come un modo per indirizzare un elemento quando lo scripting e come un modo per disegnare uno specifico elemento dai CSS.

0

(Nota:. Migrato da https://webmasters.stackexchange.com/a/30031/10884 e curato per brevità)

Se avete bisogno di saltare agli utenti di collegamenti in-page, noto anche come identificatori di frammento, è possibile impostare l'attributo id (che è used for more than just frag ids) su qualsiasi elemento. Quindi utilizzare il solito # nell'URL di un attributo href di un elemento a. Ecco un esempio:

<body> 
    <p>Despite the many 
    <a href="#benefits-of-gum-chewing">benefits</a> 
    you may experience while chewing gum, there are also many drawbacks, 
    especially with 
    <a href="http://www.example.org/sugar.html#cons">non-sugarless</a> 
    gum.</p> 
    ... 
    <section id="benefits-of-gum-chewing"> 
    <h1>Benefits of Gum Chewing</h1> 
    ... 
    </section> 
</body> 

Quando si scrivono le mie pagine, Vorrei dare un id ad ogni <section> tag (HTML5), anche se non ho intenzione di usarlo. Il valore di id è una versione compatibile con l'URL del contenuto della sua intestazione. È possibile ottenere lo stesso effetto assegnando lo stesso id a un <h1>, ecc