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


     
    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.
     
  • 相关阅读:
    Java Leetcode 两数之和
    Springboot 实现前后分离的固定响应结构 {code,data,msg} 代码及想法
    Java 通过原子类 AtomicStampedReference 实现自旋锁
    【待整理】PC端
    【Python】自动发送邮件
    【Python】调用C/C++ SDK/SO动态库
    【Python】整个项目所需的依赖包列表打包/项目迁移
    【待整理】工具
    【数据库】+Navicat 过期/14天试用期满
    【待整理】【后端】
  • 原文地址:https://www.cnblogs.com/jianglijs/p/7390168.html
Copyright © 2020-2023  润新知