用于导航的可访问更改菜单项,使用不显眼的JS,而不是jQuery,

时间:2013-12-09 12:20:53

标签: javascript css html5 accessibility semantics

我现在感觉自己是一个严肃的id10t,但我花了一周的时间寻找一种语义,可访问,JavaScript方法,在链接上保持活动样式,直到点击另一个链接。我希望这将是非常基本的东西......

该网站是一个单页,其中包含使用id / anchor标记链接到页面上各个部分的链接。

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8" /></head>
<body>

<header>
<h1>Crown Cattle Company</h1>
<ol>
<li><a href="../index.html">(Temp Home)</a></li>
<li><a href="#farm">Orielton</a></li>
<li><a href="#genetics">Wagyu</a></li>
<li><a href="#cooking">Recipes</a></li>
</ol>
</header>

<main>
<article>

<section id="orielton">
<h2>Orielton</h2>
<nav><ul>
<li><a href="#farm">Crown Cattle Company</a></li>
<li><a href="#maps">Map</a></li>
<li><a href="#contact">Contact</a></li>
</ul></nav>
<p id="farm">CCC. Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
<p id="map"><div id="map-canvas"></div></p>
<p id="contact">Contact. There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which don't look even slightly believable. If you are going to use a passage of Lorem Ipsum, you need to be sure there isn't anything embarrassing hidden in the middle of text. All the Lorem Ipsum generators on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the Internet. It uses a dictionary of over 200 Latin words, combined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always free from repetition, injected humour, or non-characteristic words etc.</p>
</section>
</article>
</main>
</body>
</html>

源文件在这里: (http://www.crowncattle.com.au/html/content%20scroll.html#farm

提前感谢你让我保持理智。这是我的头脑。干杯

2 个答案:

答案 0 :(得分:1)

在最新的浏览器中,我建议:

function lastLink(e){
    e.preventDefault();
    var _lastClicked = document.querySelector('a.lastActive');
    if (_lastClicked) {
        _lastClicked.classList.remove('lastActive');
    }
    this.classList.add('lastActive');
}

var links = document.querySelectorAll('a');

[].forEach.call(links, function(a){
    a.addEventListener('click', lastLink);
});

JS Fiddle demo

参考文献:

答案 1 :(得分:0)

如果您要查找的只是激活链接,则可以使用:

document.getElementById('linkID').focus();

当您点击其他链接时,该链接将变为焦点,原始链接将失去焦点。


分步指南

如果你有这个HTML:

<a href="http://whoknows.com">A Link</a>

您需要为其添加ID - 除非它已有ID。每页ID应该是唯一的。在这里,我使用了&#34; linkID&#34;:

<a href="http://whoknows.com" id="linkID">A Link</a>

然后,您需要在链接下方的HTML中添加此脚本:

<script>
  document.getElementById('linkID').focus();
</script>