What is href=“#” and why is it used?

前端 未结 9 707
离开以前
离开以前 2020-11-22 12:49

On many websites I see links that have href=\"#\". What does it mean? What is it used for?

9条回答
  •  野性不改
    2020-11-22 13:08

    As some of the other answers have pointed out, the a element requires an href attribute and the # is used as a placeholder, but it is also a historical artifact.

    From Mozilla Developer Network:

    href

    This was the single required attribute for anchors defining a hypertext source link, but is no longer required in HTML5. Omitting this attribute creates a placeholder link. The href attribute indicates the link target, either a URL or a URL fragment. A URL fragment is a name preceded by a hash mark (#), which specifies an internal target location (an ID) within the current document.

    Also, per the HTML5 spec:

    If the a element has no href attribute, then the element represents a placeholder for where a link might otherwise have been placed, if it had been relevant, consisting of just the element's contents.

提交回复
热议问题