This:
Code:
if (targ==Mlinks[i].childNode[0]) {do something}
if else (targ==Mlinks[i].childNode[0]) {something else}
is written wrong, it's else if not if else. And it is childNodes, not childNode. For childNodes[0], you can use firstChild instead though.
And the two conditions that you are checking for are identical anyway, so there would be no difference. If the code failed the first test, it would fail the second test. If the code passed the first test, it wouldn't consider the second test.
Now, I am having trouble following just what you want to do. I think that you want to branch on which (if any) of the Mlinks are involved.
In about the simplest of terms:
Code:
<script type="text/javascript">
if(document.getElementById)
document.onclick = function (e){
e = e? e : window.event;
var targ = e.target? e.target : e.srcElement,
Mlinks = document.getElementById('navebase').getElementsByTagName('a');
while(targ.parentNode && targ.parentNode.tagName && targ.tagName.toLowerCase() != 'a')
targ = targ.parentNode;
for (var i = Mlinks.length - 1; i > -1; --i)
if (targ == Mlinks[i]){
alert ('Mlinks number: ' + i + ' was clicked.');
return;
}
alert("Something other than an 'a' tag in the 'navebase' division was clicked");
}
else alert ("A 'version 5' or better Browser is required for this script.");
</script>
That will get you the link number 0 to however many that there are if one of them was clicked. But let's say you want to do something to that link. Then you could do:
Code:
if (targ == Mlinks[i]){
Mlinks[i].style.backgroundColor = 'red';
return;
}
or if what you want to do depends upon which link it is:
Code:
if (targ == Mlinks[i]){
if (i == 0) dosomething();
else if (i == 1) dosomethingelse();
else if (i == 2) doyetsomeotherthing();
else doadefaultthing();
return;
}
or you could just pass off the value of i to another function for processing:
Code:
if (targ == Mlinks[i]){
processLinkNum(i);
return;
}
The function:
Code:
function processLinkNum(i){
do stuff here based on what the value of i is
}
Bookmarks