I don't think that will work, Twey. At least this doesn't (no error message, just didn't work):
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
onload = function() {
for(var i = 0, es = document.getElementsByTagName('span'), n = es.length, e = es[0]; i < n; e = es[++i]) {
var m = e.className.match(/swap\((\w+),\s*(#?[\d\-]+)\)/);
if(m)
e['on' + m[1]] = (function(n) {
return function() {
var s = this.style;
var oldclr = s.backgroundColor;
s.backgroundColor = n;
n = oldclr;
};
})(m[2]);
}
e = es = null;
};
</script>
</head>
<body>
<span class="swap(click, yellow)" style="background-color: red;">Hey</span>
</body>
</html>
I did change 'td' to 'span' in the script, so that shouldn't have made any difference though, I may have missed something.
This works out well and should support any method of naming and/or setting the color that any modern browser supports:
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
span {
background-color:lightblue;
}
</style>
<script type="text/javascript">
function changeColor(el, clr){
var id, f=changeColor, c='color', r='run';
if(!el.id||!f[el.id+r]){
id=f.ids[f.ids.length]=el.id=el.id? el.id : c+'$_'+r+f.ids.length;
f[id+c]=el.style.backgroundColor;
f[id+r]=1;
}
else{id=el.id;};
el.style.backgroundColor=f[id+c]==el.style.backgroundColor? clr : f[id+c];
}
changeColor.ids=[];
</script>
</head>
<body>
<span onclick="changeColor(this, 'yellow')">Hey</span>
</body>
</html>
Bookmarks