ID があるなら活用すべきです。それと、href="javascript:..." なんてのは NN4 の遺物ですので、もう止めて下さい。
<a href="#section-1" onclick="return GAccordion.call(this, event);">ラベル 1</a>
<a href="#section-2" onclick="return GAccordion.call(this, event);">ラベル 1</a>
<a href="#section-3" onclick="return GAccordion.call(this, event);">ラベル 1</a>
<div>
<div id="section-1">...</div>
<div id="section-2">...</div>
<div id="section-3">...</div>
</div>
<script type="text/javascript">
// IE 5.5+
function GAccordion (e) {
var c = e.currentTarget || this;
var d = c.ownerDocument;
var s;
if ((s = c.getAttribute('href' /*@cc_on, 2@*/))) {
var id = s.slice (1);
var div;
if ((div = d.getElementById(id))) {
id = /^(.*)(\d+)$/.exec (id);
if (id[2]) {
var prefix = id[1];
var n;
for (n = div.parentNode.firstChild; n; n = n.nextSibling) {
if (n.nodeType === 1 && n.id && n.id.indexOf(prefix) === 0) {
n.style.display = 'none';
}
}
div.style.display = ''; // CSSOM
}
}
}
return false;
}
</script>
初期化はご自分で。どの道、スクリプトが動かない利用者は一定数いますので、そういった場合の代替措置は絶対に必要です。それができないとしたら、下手に仕掛けなど設けない方が無難です。
---
最近のブラウザなら(手抜き)。
<style type="text/css">
@media only screen {
*[role~="tab"] {
cursor: pointer;
}
*[role~="tab"][tabindex="0"] {
font-weight: bold;
color: #800;
}
*[role~="tabpanel"][aria-hidden="true"] {
display: none;
}
}
</style>
<ul role="tablist">
<li role="tab" id="tab-1" tabindex="0">ラベル 1</li>
<li role="tab" id="tab-2" tabindex="-1">ラベル 2</li>
<li role="tab" id="tab-3" tabindex="-1">ラベル 3</li>
</ul>
<div role="group">
<div role="tabpanel" aria-labelledby="tab-1" aria-hidden="false">...</div>
<div role="tabpanel" aria-labelledby="tab-2" aria-hidden="true">...</div>
<div role="tabpanel" aria-labelledby="tab-3" aria-hidden="true">...</div>
</div>
<script type="application/javascript">
if (document.implementation &&
// document.implementation.hasFeature ('Selectors-API', '1.0') &&
document.implementation.hasFeature ('Events', '2.0') &&
document.implementation.hasFeature ('HTML', '2.0')) {
var GAccordion = {
handleEvent: function (e) {
var t = e.target;
var d = t.ownerDocument;
if (this.isTab (t)) {
var tab = t;
switch (e.type) {
case 'focus' :
this.activate (tab, t.parentNode.querySelector ('*[role~="tab"][tabindex="0"]'));
return;
case 'keydown' :
switch (e.keyCode) {
case 0x26: // DOM_VK_UP
for (t = tab; t = t.previousSibling; ) {
if (this.isTab (t)) {
t.focus ();
break;
}
}
return;
case 0x28: // DOM_VK_DOWN
for (t = tab; t = t.nextSibling; ) {
if (this.isTab (t)) {
t.focus ();
break;
}
}
return;
}
}
}
},
isTab: function (n) {
return n.nodeType === Node.ELEMENT_NODE && n.getAttribute ('role') === 'tab';
},
activate: function (tab1, tab2) {
var d = tab1.ownerDocument;
var tp = '*[role~="tabpanel"]';
tab2.setAttribute ('tabindex', '-1');
Array.forEach (d.body.querySelectorAll (tp + '[aria-labelledby~="' + tab2.id + '"]'), function (tabpanel) {
tabpanel.setAttribute ('aria-hidden', 'true');
});
tab1.setAttribute ('tabindex', '0');
Array.forEach (d.body.querySelectorAll (tp + '[aria-labelledby~="' + tab1.id + '"]'), function (tabpanel) {
tabpanel.setAttribute ('aria-hidden', 'false');
});
}
};
document.addEventListener ('focus', GAccordion, true);
document.addEventListener ('keydown', GAccordion, false);
}
</script>
お礼
ご回答ありがとうございます。