简体   繁体   English

<a>没有 href="#" 的脚本链接</a>

[英]<a> script links without href="#"

My web application uses a lot of <a href="#">Perform client script action</a> anchor links which are used for scripting (a habit I picked-up from my days as a Dreamweaver user well over 10 years ago) and now I'm beginning to question if this is necessary.我的 Web 应用程序使用了很多用于编写脚本的<a href="#">Perform client script action</a>锚链接(这是我 10 多年前作为 Dreamweaver 用户养成的习惯)现在我开始怀疑这是否有必要。

I don't actually know why <a href="#"> is used in the first place - my guess is the presence of the href attribute causes browsers to apply the :link and :visited psuedo-classes which simplifies stylesheets.我实际上并不知道为什么首先使用<a href="#"> - 我的猜测是href属性的存在导致浏览器应用:link:visited伪类,从而简化了样式表。 It also makes the browser implicitly apply the cursor: pointer property which may be desirable.它还使浏览器隐式应用cursor: pointer可能需要的属性。

I use jQuery (after a brief stint with MooTools) and the links work just as well without the href="#" attribute and value, so should I just remove the attribute entirely and modify my stylesheet to compensate for the removal of the :link psuedo-class?我使用 jQuery(在短暂使用 MooTools 之后)并且链接在没有href="#"属性和值的情况下也能正常工作,所以我应该完全删除该属性并修改我的样式表以补偿删除:link伪类? Is there anything else I might be missing, any kind of semi-documented voodoo that the href attribute imbues the anchor element?还有什么我可能会遗漏的吗,href 属性赋予锚元素的任何一种半文档化的巫术?

<a> stands for anchor <a>代表锚点

If you include the [href] attribute on an <a> element, it is an anchor that points to a location, which means that you could go to a new page, a particular fragment of the current page (hence the # being called the fragment identifier), or a particular fragment of a new page.如果在<a>元素上包含[href]属性,它是一个指向某个位置的锚点,这意味着您可以转到新页面,即当前页面的特定片段(因此#被称为片段标识符),或新页面的特定片段。

<a> elements without an [href] attribute were historically assigned a [name] attribute, which could be used as the destination of the fragment identifier.没有[href]属性的<a>元素在历史上被分配了一个[name]属性,它可以用作片段标识符的目的地。 Browsers later added support for linking to any item's [id] attribute, and this is now the preferred method for linking to a document fragment.浏览器后来添加了对链接到任何项目的[id]属性的支持,现在这是链接到文档片段的首选方法。

What does this mean for standalone <a> elements?这对独立的<a>元素意味着什么?

An a[href] element is a link (which is why they are matched with :link in css). a[href]元素是一个链接(这就是它们与:link在 css 中匹配的原因)。 links are clickable.链接是可点击的。 An a element without the [href] attribute is otherwise just a placeholder for where a link might otherwise have been placed , and not clickable, nor are they in the tabbing order of the page. 没有[href]属性的a元素只是一个占位符,用于放置链接的位置,不可点击,也不符合页面的 Tab 键顺序。

If you want your links to be keyboard navigable which is important for accessibility ( WAI-ARIA ), you'll need to do one of the following:如果您希望您的链接可通过键盘导航,这对于可访问性 ( WAI-ARIA ) 很重要,您需要执行以下操作之一:

  • change the element to <button type="button">将元素更改为<button type="button">
  • keep the [href] attribute保留[href]属性
  • add [tabindex="0"] and one of [role="button"] or [role="link"] (and possibly some styling)添加[tabindex="0"][role="button"][role="link"]之一(可能还有一些样式)

More information about the [role] attribute can be found in the Roles Model section of the WAI-ARIA docs .有关[role]属性的更多信息可以在WAI-ARIA 文档的角色模型部分找到。

Changing the markup更改标记

If you don't have a reason to keep the [href] attribute, you might as well be using a <button> element:如果你没有理由保留[href]属性,你还不如使用<button>元素:

<button type="button">
        ^^^^^^^^^^^^^

The [type] attribute is used to make the element a generic button, otherwise <button> will default to [type="submit"] , which may not be desirable as it could trigger form submission. [type]属性用于使元素成为通用按钮,否则<button>将默认为[type="submit"] ,这可能是不可取的,因为它可能触发表单提交。

If you can't use a <button> (usually occurs when the inner markup must contain a <div> ) you can fake a <button> using:如果您不能使用<button> (通常发生在内部标记必须包含<div>时),您可以使用以下方法伪造<button>

<div role="button" tabindex="0">Some clickable text</div>

You'll need to listen for keypress events and trigger click events for Enter and Space .您需要监听keypress事件并触发EnterSpaceclick事件。

Keeping the markup保留标记

If you're keeping the <a> element and its [href] attribute, there are a number of options for its value.如果您保留<a>元素及其[href]属性,则其值有多种选择。

A real link一个真实的链接

Ex前任

  • <a href="/some/location/for/users/without/js">
  • <a href="#document-fragment">

If you need to provide support for users with JS disabled, you might as well direct them to a page that performs equivalent functionality without JS.如果您需要为禁用 JS 的用户提供支持,您不妨将他们引导到一个页面,该页面在没有 JS 的情况下执行等效功能。

By extension, this also includes providing document fragment links to link to the content within the same document.通过扩展,这还包括提供文档片段链接以链接到同一文档中的内容。 For example, a toggleable region may be marked up as:例如,可切换区域可以标记为:

<a href="#more" class="toggleable">Read More</a>
<div id="more">Lorem ipsum dolor sit amet</div>

So that with JS the region can be collapsed and expanded, and without JS the link will take the user to the appropriate content on the page.因此,使用 JS 可以折叠和展开区域,而没有 JS,链接将把用户带到页面上的适当内容。

A dud href一个无用的 href

Ex前任

  • <a href="#">
  • <a href="javascript:void(0)">
  • <a href="about:blank">

If you're preventing the default behavior behind-the-scenes in JavaScript, and you're not supporting users with JS disabled, you can use a "dud" href value to keep the link in the tabbing order and automatically enable Enter to trigger the click event.如果你要阻止 JavaScript 中的默认行为,并且你不支持禁用 JS 的用户,你可以使用“dud”href 值来保持链接的跳转顺序并自动启用Enter触发click事件。 You should add [role="button"] as semantically the <a> tag is no longer being used as a link, but as a button.应该添加[role="button"]因为<a>标签在语义上不再用作链接,而是用作按钮。

<a href="#" role="button">Some clickable text</a>

Personally I prefer to use href="javascript:void(null);"我个人更喜欢使用href="javascript:void(null);" , to give the browser an href that won't mess up any other use of hashes. , 为浏览器提供一个 href,它不会混淆哈希的任何其他用途。

The only difference I've noticed about href-less links is that the browser will not underline them by default, so just add that style in and you should be good.我注意到 href-less 链接的唯一区别是浏览器默认情况下不会为它们添加下划线,因此只需添加该样式即可,您应该会很好。

I got the same issue with react js.我在 React js 中遇到了同样的问题。

Line 273:  Links must not point to "#". Use a more descriptive href or use a button instead  jsx-a11y/href-no-hash

like this to remove the error I have used the像这样删除我使用过的错误

before:前:

 <a 
      href="#"
      className="banner-signup signup-btn envy-font-sans-bold"
      onClick={(e) => this.gonext()}
    >
     NEXT
    </a>

used the href="#/" on the place of href="#"在 href="#" 的地方使用了 href="#/"

 <a 
      href="#/"
      className="banner-signup signup-btn envy-font-sans-bold"
      onClick={(e) => this.gonext()}
    >
     NEXT
    </a>

use in html在 html 中使用

<a id="calendar" href="javascript:void(0)">
        <div class="icon-w">
          <div class="fa fa-calendar"></div>
        </div>
     <span>Calendar</span>
</a> 

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

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