为什么在空的锚标签hrefs中使用#约定? [英] Why is the # convention used in empty anchor tag hrefs?

查看:77
本文介绍了为什么在空的锚标签hrefs中使用#约定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道,最好不要留空锚标签.在jQuery和其他语法中,我注意到每个人通常都使用#来填补空白(<a href='#'>anchor text</a>).这个字符比填充其他字符好还是坏? (例如<a href='$'>anchor text</a>).我没有理由要这样做,但似乎除了约定以外,没有其他理由要这样做.为什么在空的锚标记hrefs中使用#约定?

I understand that it's a good idea not to leave empty anchor tags. In jQuery and other syntaxes I've noticed everyone typically uses a # to fill the gap (<a href='#'>anchor text</a>). Is this character any better or worse than filling it with anything else? (e.g. <a href='$'>anchor text</a>). I have no reason to want to do this, but seemingly no reason aside from convention to do it the other way either. Why is the # convention used in empty anchor tag hrefs?

推荐答案

这是因为URL中的#字符引用了本地页面.

This is because the # character in a URL references the local page.

它用于页面内的命名锚(或任何ID),因此链接可以直接跳到该区域.

It is used for named anchors (or any ID) within a page, so a link can jump directly to that area.

Wikipedia 称其为fragment identifier,并说:

片段标识符(如果存在)指定整个资源或文档中的一部分或位置.与HTTP结合使用时,通常会在页面中指定一个区域或位置,浏览器可能会滚动以显示页面的该部分.

The fragment identifier, if present, specifies a part or a position within the overall resource or document. When used with HTTP, it usually specifies a section or location within the page, and the browser may scroll to display that part of the page.


作为一个实际示例-指向维基百科的链接具有片段标识符(始终在URL的末尾):


As a practical example - this link to wikipedia has a fragment identifier (always at the end of the URL):

http://en.wikipedia.org/wiki/Uniform_Resource_Locator#Syntax

页面中有一个<span id="Syntax" ...标记,浏览器直接跳转到该标记.

In the page, there is a <span id="Syntax" ... tag, and the browser jumps directly to it.

这篇关于为什么在空的锚标签hrefs中使用#约定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆