• 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.
     
  • 相关阅读:
    以太坊虚拟机介绍
    以太坊源码学习 – EVM
    Visual Studio Code 常用快捷键
    深入了解以太坊虚拟机第4部分——ABI编码外部方法调用的方式
    深入了解以太坊虚拟机第3部分——动态数据类型的表示方法
    深入了解以太坊虚拟机第2部分——固定长度数据类型的表示方法
    深入了解以太坊虚拟机
    选取文档元素的方法
    flex布局
    什么是功能需求设计文档
  • 原文地址:https://www.cnblogs.com/jianglijs/p/7390168.html
Copyright © 2020-2023  润新知