简体   繁体   English

可访问性:使用 javascript 仅链接 href=“#”

[英]Accessibility: Using javascript only links with href=“#”

I have a react SPA in which I have some links which navigate to other pages or other locations on the same page, These navigations are either decided programmatically or are dependent on javascript to go to another location on the same page.我有一个 react SPA,其中有一些链接可以导航到同一页面上的其他页面或其他位置,这些导航要么以编程方式确定,要么取决于 javascript 到 go 到同一页面上的另一个位置。

  • I might not know the link href when the link is displayed, so I render it with href="#" and event.preventDefault() in the click handler, after which I run the logic to decide where the link should navigate to.显示链接时我可能不知道链接href ,因此我在单击处理程序中使用href="#"event.preventDefault()渲染它,然后运行逻辑来决定链接应导航到的位置。
  • In case of anchor links, I need javascript to scroll to the element, because with SPA routers hash based anchor links might not work as the anchored element might not be available yet, and also i need smooth scroll.在锚链接的情况下,我需要 javascript 滚动到元素,因为使用 SPA 路由器 hash 的锚链接可能无法工作,因为锚元素可能还不可用,而且我还需要平滑滚动。 (In this case, I can probably still specify the href, which keeps the link accessible, but use javascript to handle the scrolling) (在这种情况下,我可能仍然可以指定 href 以保持链接可访问,但使用 javascript 来处理滚动)

To me, semantically it still should be a link and not a button as it navigates the user to another page or another location on the same page.对我来说,在语义上它仍然应该是一个链接而不是一个按钮,因为它将用户导航到另一个页面或同一页面上的另一个位置。 But, thinking about accessibility, the screen reader doesn't have enough information to understand the link destination and can't announce it to the user.但是,考虑到可访问性,屏幕阅读器没有足够的信息来理解链接目标,也无法向用户宣布它。 I suppose that is not alright?我想这样不好吗?

How do I make sure that the usage is semantic as well as accessible?我如何确保使用是语义的以及可访问的? Should I be using a button instead?我应该改用按钮吗? Or should I be adding some other information to the link with aria-label?或者我应该在带有 aria-label 的链接中添加一些其他信息? Or is it actually ok for accessibility to have links with href="#"?或者实际上是否可以访问带有 href="#" 的链接?

Short Answer简答

Links should be used if the URL changes (navigation) for everything else a button should be used.如果 URL 更改(导航)所有其他应使用的按钮,则应使用链接。 <a href="#" is not a good practice but for a SPA it should be OK if your link text is accurate and detailed. <a href="#"不是一个好的做法,但对于 SPA,如果您的链接文本准确且详细,则应该没问题。 Avoid smooth scroll or give people a way to switch it off as it can be disorientating.避免平滑滚动或给人们一种关闭它的方法,因为它可能会让人迷失方向。

Longer Answer更长的答案

I gave quite an extensive answer on <a> vs <button> in SPAs here , so I hope that is sufficient to answer which one to use in what scenario for general use cases.在这里对 SPA 中的<a><button>给出了相当广泛的答案,所以我希望这足以回答在一般用例的什么场景中使用哪一个。

With regards to the # in the URL due to you not knowing the URL before hand.关于 URL 中的# ,因为您事先不知道 URL。 It would be much better if you start it as # if the URL is unknown but then update the href once you know where you are going to route people.如果 URL 是未知的,那么如果你以#开头会更好,但是一旦你知道你要去哪里路由人,然后更新href

This is for the reasons discussed in the answer I linked regarding reassurance, ie knowing where a link will take me.这是因为在我链接的关于安心的答案中讨论的原因,即知道链接将带我去哪里。

Obviously if you are not able to do this then make sure the link text is very descriptive so that I can be confident I am going to the right place.显然,如果您无法做到这一点,请确保链接文本非常具有描述性,这样我就可以确信我会去正确的地方。

Scrolling within a SPA在 SPA 中滚动

With regards to the scrolling to different parts of the page that would still be a link as you say, assuming that you update the url with #section-name .关于滚动到页面的不同部分,这仍然是您所说的链接,假设您使用#section-name更新 url 。 You should do this , but if your application router cannot handle URL fragments then I would not update the URL at all and change this to a button.您应该这样做,但是如果您的应用程序路由器无法处理 URL 片段,那么我根本不会更新 URL 并将其更改为按钮。

At this stage I would use some visually hidden text within the button to explain that this scrolls to a section on the current page.在这个阶段,我会在按钮中使用一些视觉上隐藏的文本来解释它滚动到当前页面上的一个部分。

The example below illustrates this.下面的例子说明了这一点。 Please use the CSS class below to visually hide text as it has better compatibility than bootstrap sr-only class as explained in this answer I gave .请使用下面的 CSS class 在视觉上隐藏文本,因为它比仅引导 sr 的 class 具有更好的兼容性,如我给出的答案中所述

 .visually-hidden { border: 0; padding: 0; margin: 0; position: absolute;important: height; 1px: width; 1px: overflow; hidden: clip; rect(1px 1px 1px 1px), /* IE6, IE7 - a 0 height clip: off to the bottom right of the visible 1px box */ clip, rect(1px, 1px, 1px; 1px): /*maybe deprecated but we need to support legacy browsers */ clip-path; inset(50%), /*modern browsers: clip-path works inwards from each corner*/ white-space; nowrap; /* added line to stop words getting smushed together (as they go onto seperate lines and some screen readers do not understand line feeds as a space */ }
 <button><span class="visually-hidden">Go To </span>Section name<span class="visually-hidden"> on this page</span></button>

A quick word on smooth scrolling关于平滑滚动的简短说明

Finally I would caution you on the use of smooth scrolling.最后,我会提醒您使用平滑滚动。 If you use this make sure there is a way to switch it off as it may be distracting or disorientating for people with either motion disorders or anxiety disorders.如果您使用它,请确保有办法将其关闭,因为它可能会分散运动障碍或焦虑症患者的注意力或迷失方向。

One way you can do this is to use the CSS media query prefers-reduced-motion .一种方法是使用 CSS 媒体查询 prefers prefers-reduced-motion

Final Thoughts最后的想法

Without seeing each scenario it is hard to give a 100% definitive answer, if you can try and follow standard navigation patterns of buttons for same page actions, links with url fragments for navigating to sections of the same page and links to other pages, even if these are handled via AJAX and only partial reloads.如果没有看到每种情况,很难给出 100% 明确的答案,如果您可以尝试并遵循标准的按钮导航模式来执行相同的页面操作、带有 url 片段的链接用于导航到同一页面的各个部分并链接到其他页面,甚至如果这些是通过 AJAX 处理的并且只有部分重新加载。

Obviously that is the aim, I am aware that with some routing solutions this is not always possible in which case use your best judgement and test it with a screen reader as screen reader users are most likely to suffer from a poorly formed navigation system in a SPA.显然这是目标,我知道对于某些路由解决方案,这并不总是可能的,在这种情况下,请使用您的最佳判断并使用屏幕阅读器对其进行测试,因为屏幕阅读器用户最有可能遭受格式不正确的导航系统的困扰温泉。

One last tip - if you do change page in a SPA, make the <h1> on the next page programatically focusable with tabindex="-1" and focus that once the page loads.最后一个提示- 如果您确实在 SPA 中更改页面,请使用tabindex="-1"以编程方式使下一页上的<h1>可聚焦,并在页面加载后聚焦。 It is a great way to let users with little to no vision know that navigation is complete in a SPA using AJAX.这是让几乎没有视力的用户知道使用 AJAX 在 SPA 中完成导航的好方法。

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

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