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

前端 未结 9 706
离开以前
离开以前 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:18

    The problem with using href="#" for an empty link is that it will take you to the top of the page which may not be the desired action. To avoid this, for older browsers or non-HTML5 doctypes, use

    <a href="javascript:void(0)">Goes Nowhere</a>
    
    0 讨论(0)
  • 2020-11-22 13:19

    About hyperlinks:

    The main use of anchor tags - <a></a> - is as hyperlinks. That basically means that they take you somewhere. Hyperlinks require the href property, because it specifies a location.

    Hash:

    A hash - # within a hyperlink specifies an html element id to which the window should be scrolled.

    href="#some-id" would scroll to an element on the current page such as <div id="some-id">.

    href="//site.com/#some-id" would go to site.com and scroll to the id on that page.

    Scroll to Top:

    href="#" doesn't specify an id name, but does have a corresponding location - the top of the page. Clicking an anchor with href="#" will move the scroll position to the top.

    See this demo.

    This is the expected behavior according to the w3 documentation.

    Hyperlink placeholders:

    An example where a hyperlink placeholder makes sense is within template previews. On single page demos for templates, I have often seen <a href="#"> so that the anchor tag is a hyperlink, but doesn't go anywhere. Why not leave the href property blank? A blank href property is actually a hyperlink to the current page. In other words, it will cause a page refresh. As I discussed, href="#" is also a hyperlink, and causes scrolling. Therefore, the best solution for hyperlink placeholders is actually href="#!" The idea here is that there hopefully isn't an element on the page with id="!" (who does that!?) and the hyperlink therefore refers to nothing - so nothing happens.

    About anchor tags:

    Another question that you may be wondering is, "Why not just leave the href property off?". A common response I've heard is that the href property is required, so it "should" be present on anchors. This is FALSE! The href property is required only for an anchor to actually be a hyperlink! Read this from w3. So, why not just leave it off for placeholders? Browsers render default styles for elements and will change the default style of an anchor tag that doesn't have the href property. Instead, it will be considered like regular text. It even changes the browser's behavior regarding the element. The status bar (bottom of the screen) will not be displayed when hovering on an anchor without the href property. It is best to use a placeholder href value on an anchor to ensure it is treated as a hyperlink.

    See this demo demonstrating style and behavior differences.

    0 讨论(0)
  • 2020-11-22 13:26

    The href attribute defines the URL of the resource of a link. If the anchor tag does not have href tag then it will not become hyperlink. The href attribute have the following values:

    1. Absolute path: move to another site like href="http://www.google.com"
    2. Relative path: move to another page within the site like herf ="defaultpage.aspx"
    3. Move to an element with a specified id within the page like href="#bottom"
    4. href="javascript:void(0)", it does not move anywhere.
    5. href="#" , it does not move anywhere but scroll on the top of the current page.
    6. href= "" , it will load the current page but some browsers causes forbidden errors.
    
    Note: When we do not need to specified any url inside a anchor tag then use 
    <a href="javascript:void(0)">Test1</a>
    
    0 讨论(0)
提交回复
热议问题