Is there an easy way to add a bit about also setting the ancestor li tags to .active?
<script language="javascript" type="text/javascript">
function setActive() {
aObj = document.getElementById('toc').getElementsByTagName('a');
for(i=0;i<aObj.length;i++) {
if(document.location.href.indexOf(aObj[i].href)>=0) {
aObj[i].className='active';
}
}
}
window.onload = setActive;
</script>
It depends on what you mean. I don’t get a full picture of what’s going on from your question. From the looks of it you’re grabbing an element with the id of “toc”, so I’m guessing this toc contains
LIelements as well asAelements. In which case you could use an asterisk ingetElementsByTagName, which could be slower depending on how many other elements are there.If you’re asking for ancestral LI higher up in the document tree, this is what
you would want: