简体   繁体   English

辅助功能和焦点/跳转锚点

[英]Accessibility and focus/jump to anchor

I'd like to jump the focus to an anchor due to some accessibility need. 由于某些可访问性的需求,我想将重点放在锚点上。

The reason of this is reading browsers. 这样做的原因是阅读浏览器。 When I click the link "Focus to anchor", I want the browser starting to read the page from the anchor, but for that I need to focus it. 当我单击链接“聚焦到锚点”时,我希望浏览器开始从锚点读取页面,但是为此,我需要集中精力。

I also want the tab navigation starting from this anchor. 我还希望标签导航从此锚点开始。 I don't want to use tabindex, which is not really a good way to use it. 我不想使用tabindex,这并不是使用它的好方法。 I don't want to use the #hash in the URL as well, I don't need any auto-scroll in my page. 我也不想在URL中使用#hash,也不需要在页面中进行任何自动滚动。

Unfortunately today my code below is not working. 不幸的是,今天我的以下代码无法正常工作。 This is just an example to illustrate my need. 这只是说明我需要的一个例子。

<div id="content">

  <p>Paragraph...</p>
  <p>Paragraph...</p>
  <a href="#">A link</a>
  <p>Paragraph...</p>
  <p>Paragraph...</p>

  <a name="myAnchor"></a>

  <p>Paragraph...</p>
  <p>Paragraph...</p>
  <a href="#">A link</a>
  <p>Paragraph...</p>
  <p>Paragraph...</p>

  <a href="javascript:;" id="focusToAnchor">Focus to anchor</a>

</div>
<script type="text/javascript">
    $('#focusToAnchor').click(function() { $('a[name="myAnchor"]').focus(); });
</script>

Any suggestion? 有什么建议吗? Thanks 谢谢

first you need to wrap the code in a $(document).ready() function next I would change the name attribute to an id and use a normal selector in the click handler 首先,您需要将代码包装在$(document).ready()函数中,然后将name属性更改为id并在点击处理程序中使用常规选择器

I would also use the hashbang as a href to maintain valid html, but prevent the default in the click handler 我还可以将hashbang用作href来维护有效的html,但可以防止点击处理程序中的默认设置

<a id="myAnchor"></a>
$(document).ready(function(){
   $('#focusToAnchor').click(function(e) {
       e.preventDefault();

       $("#myAnchor").focus(); 
   });
});

Update With Fiddle 用小提琴更新

http://jsfiddle.net/hxUpQ/11/ it doesn't seem to trigger the style, but the element is definately getting focused, as it is firing a focus event. http://jsfiddle.net/hxUpQ/11/它似乎并没有触发样式,但是由于它触发了焦点事件,因此该元素肯定会变得聚焦。 I also tested to see if anything else was suddenly stealing the focus but no amount of my testing showed that was the case. 我还进行了测试,看是否还有其他东西突然失去了焦点,但是没有多少测试表明是这种情况。

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

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