简体   繁体   English

锚标记中href =“###”的用法是什么?

[英]What is the use of href=“###” in anchor tag

I see these lines of code in some professional developer project: 我在一些专业的开发人员项目中看到了这些代码:

<a href="###">
    ...
</a>

What is the use of it? 有什么用?

代码图片

I think this is same as <a href="#">Go to link</a> that person just used three "###" instead of one "#". 我认为这与<a href="#">Go to link</a>相同,该人只使用了三个“###”而不是一个“#”。 we can use more # if we want. 如果我们想要,我们可以使用更多#。

That's usually written when you want your anchor tag to not change the href . 当您希望锚标记不更改href时,通常会写入。 For instance if you want to attach an event on it later on. 例如,如果您想稍后在其上附加事件。

It doesn't matter how many # you are using. 你正在使用多少#并不重要。 The href='#' will make the page jump to the top of the page if clicked. 如果单击, href='#'将使页面跳转到页面顶部。

My preferred way is doing <a href="javascript:void(0);" 我首选的方法是<a href="javascript:void(0);" . That way the click does absolutely nothing, instead of jumping the page up. 这样点击完全没有任何意义,而不是跳过页面。

As Bhuiyan said, it is a variation on the href="#" trick... 正如Bhuiyan所说,这是href="#"技巧的一个变种......

Goal 目标

So, to explain that trick. 所以,解释这个伎俩。 The trick is to work around the fact that Anchor tags are only rendered as links if they have a target to link to (see this example ). 诀窍是解决这样一个事实:如果Anchor标签有一个要链接的目标,它们只会呈现为链接(参见本 )。 The goal is to create a link that looks like a link, but doesn't actually go anywhere. 目标是创建一个看起来像链接的链接,但实际上并没有去任何地方。

How it works 这个怎么运作

The href="#" idiom is taking advantage of the fact that anchors can specify a specific element as a target by using the href="#{other element identifier}]" notation. href="#"惯用法利用了锚点可以通过使用href="#{other element identifier}]"符号将特定元素指定为目标的事实。 When you do this the browser will not redirect or reload the page, but instead scroll to that element. 执行此操作时,浏览器不会重定向或重新加载页面,而是滚动到该元素。 So when you specify href="#" you are essentially telling the browser that this is a link, but since there is no valid target element please don't do anything when it is clicked. 因此,当您指定href="#"您实际上是告诉浏览器这是一个链接,但由于没有有效的目标元素,请在单击时不要执行任何操作。

Note: It would work just as effectively to say href="#mybogusid" but then you would see #mybogusid appended to the url. 注意:它会像href="#mybogusid"一样有效,但是你会看到#mybogusid附加到url。 Gross. 毛。

TL;DR of it all: <a href="###"> is just a way to make the browser render the anchor tag like a link, but not navigate anywhere on click. TL;全部的DR<a href="###">只是让浏览器像锚链一样渲染锚标签,但不能在点击时随地导航的方法。

What is the use of it? 有什么用?

Another answer in this questions is good. 这个问题的另一个答案是好的。

The first thing about "Anchor Tag"... 关于“锚标记”的第一件事......

Just like quotes I mentioned, she use ### in particular to create a links that don't go anywhere, and to do that the href attribute must have a value. 就像我提到的引号一样,她特别使用###来创建一个不会去任何地方的链接,为此, href属性必须有一个值。 Please read w3.org mentioned about that. 请阅读提及的w3.org

Browsers render default styles for elements and will change the default style of an anchor tag that doesn't have the href property. 浏览器为元素呈现默认样式,并将更改不具有href属性的锚标记的默认样式。 Instead, it will be considered like regular text. 相反,它将被视为常规文本。 It even changes the browsers behavior in regards to the element. 它甚至改变了与元素有关的浏览器行为。 The status bar (bottom of the screen) will not be displayed when hovering an anchor without the href property. 悬停没有href属性的锚点时,不会显示状态栏(屏幕底部)。 It is most optimal, then, to use a placeholder href value on an anchor to ensure it is treated as a hyperlink. 因此,最好在锚点上使用占位符href值以确保将其视为超链接。

I've often seen <a href="#"> , and <a href="##"> , a hashtag - # within a hyperlink specifies an html element id to which the window should be scrolled. 我经常看到<a href="#"><a href="##"> ,一个标签 - 超链接中的#指定了一个应该滚动窗口的html元素id。

href="#some_id" would scroll to an element on the current page such as <div id="some_id"> . href="#some_id"将滚动到当前页面上的元素,例如<div id="some_id">

href="//example.com/#some_id" would go to example.com and scroll to the id on that page. href="//example.com/#some_id"将转到example.com并滚动到该页面上的id。 href="#" doesn't specify an id name, but does have a corresponding location - the top of the page. href="#"没有指定id名称,但确实有相应的位置 - 页面顶部。 Clicking an anchor with href="#" will move the scroll position to the top. 单击带有href="#"的锚点会将滚动位置移动到顶部。

So, when you use <a href="###"> this create a links but it doesn't go anywhere. 因此,当您使用<a href="###">这创建链接但它不会去任何地方。 You can use it from "##","###" and more of hashtag to create a links like a "Hyperlinks" but they take you nowhere. 您可以使用"##","###"和更多主题标签来创建像“超链接”这样的链接,但它们无处可用。

My Conclusion : 我的结论:

So, What is the use of it ? 那么,有什么用呢?

You use it, when you need a Hyperlinks that don't go anywhere. 当你需要一个不能去任何地方的超级链接时,你可以使用它。 It's just tell the browsers to change the style to an anchor tag. 它只是告诉浏览器将样式更改为锚标记。 :) :)

Check this demo... 查看此演示...

It was very useful when i had button and js click event. 当我有按钮和js点击事件时非常有用。 With '#' it scrolled to top every time i pressed the button, but with '###' it stayed in place as needed! 每次按下按钮时,“#”都会滚动到顶部,但是按“###”它会根据需要保持原位!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM