I've modified the function you've provided a bit. If you notice you can see that in the below code I've avoided the use of 'getAttribute()'. If you check 'getAttribute()' has some other issues also in IE environment. So it would be better if the developer use the dot(.) notation to access the attributes (properties) of any kind of HTML element(s) in JavaScript, which is also short compared to the usage of 'getAttribute()'. So that main issue in your scrip was not 'onclick' events has some issues in IE but the usage of 'getAttribute()'.
Another shortcoming I thought about the script is the way you've accessed the anchor elements. You've accessed all the anchor elements available in your page, event it may be very few number we don't have to do that. Instead we can make the scope of the filtering of anchor elements very less if you can give an "ID" to the div element in which the menu anchor elements lies. In this case you'll only take the anchor elements which is in the div element designated for the menu section. Please do have a look at the following source.
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Untitled Document</title>
<style type="text/css">
.indentmenu {
font: 12px Arial;
overflow: hidden;
width: 100%
}
.indentmenu ul {
background: #ccc center center repeat-x;
border: 1px solid #333;
border-width: 1px 0;
float: left;
margin: 0;
padding: 0;
width: 100%
}
.indentmenu ul li {
display: inline
}
.indentmenu ul li a, .t_off {
color: #000;
float: left;
padding: 5px 11px;
text-decoration: none
}
.indentmenu ul li a:visited {
color: #000
}
.indentmenu ul li a:hover {
color: #000 !important;
text-decoration: underline
}
.indentmenu ul li .t_on {
background: red center center no-repeat;
color: #000 !important;
padding-bottom: 4px;
padding-top: 6px
}
</style>
<script type="text/javascript">
function changeClass(obj){
var lnks = document.getElementById('hmenu').getElementsByTagName('a');
for (var i = 0; i < lnks.length; ++i) {
if (lnks[i].className === "t_on") {
lnks[i].className = "t_off";
}
}
obj.className = "t_on";
}
</script>
</head>
<body>
<div id="hmenu" class="indentmenu">
<ul>
<li>
<a href="#" class="t_off" onclick="changeClass(this);">Menu 1</a>
</li>
<li>
<a href="#" class="t_on" onclick="changeClass(this);">Menu 2</a>
</li>
<li>
<a href="#" class="t_off" onclick="changeClass(this);">Menu 3</a>
</li>
</ul>
</div>
</body>
</html>
I've highlighted all the changes I've made.
Hope this helps.
Bookmarks