Table Highlight

liunx

Guest
I have this code.<br />
<br />
<table border="1" onClick="location.href='http://www.webdeveloper.com/forum/archive/index.php/main.html';"><tr><td>Main</td></tr></table> <br />
<br />
When a user clicks on the table it will take them to main.html.<br />
I would like it so that when the user points to anywhere on the table, the table changes colout to green, and when clicked on turns red.<br />
<br />
Any idea?<!--content-->First of all, you need to make the text within the cell a link. What I gave you is java script and will not run for 13% of the internet, so you need this link as a backup. Also, you should not be using a table for this. I suggest using a <div> instead. For border use this code style="border:1px solid #000000;". Now for what you asked<br />
<br />
onMouseOver="style.backgroundColor='#ff3300';" onMouseOut="style.backgroundColor='#ffffff';"<br />
onClick="location.href='http://www.webdeveloper.com/forum/archive/index.php/main.html';style.backgroundColor='#ff3300';"<br />
<br />
Put that in tag of whatever element you are using, and drop the current 'onClick' atrribute<!--content--><table> is for tabular data.<br />
If you need to add a link use <a href=http://www.webdeveloper.com/forum/archive/index.php/"yourlink.html"><br />
Then you can use :hover pseudo-class to apply different style when a cursor is over the link.<br />
<br />
Your coding approach is akin a surgeon removing tonsils through patient's anus (:D :D :D I know that is a graphic comparison, but hopefully it will help in understanding the principle :D :D :D )<!--content-->Originally posted by Vladdy <br />
<table> is for tabular data.<br />
If you need to add a link use <a href=http://www.webdeveloper.com/forum/archive/index.php/"yourlink.html"><br />
Then you can use :hover pseudo-class to apply different style when a cursor is over the link.<br />
<br />
Your coding approach is akin a surgeon removing tonsils through patient's anus (:D :D :D I know that is a graphic comparison, but hopefully it will help in understanding the principle :D :D :D ) I am the one who gave him that onclick="location.href" code lol. But I told him to put a link within whatever he is using as a container for those without js. Fred and I talked about this method, and we disgussed modifying a normal link with css to appear as if it were a block element.<br />
<!-- m --><a class="postlink" href="http://quasi-ke.servebeer.com/test.html">http://quasi-ke.servebeer.com/test.html</a><!-- m --><br />
here is the code I posted before --^, It runs with js disabled and it runs in lynx... woopy.<br />
<br />
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"><br />
<html><br />
<head><br />
<title>Block test</title><br />
</head><br />
<body><br />
<div onMouseOver="style.backgroundColor='#cccccc';" onMouseOut="style.backgroundColor='#ffffff';"<br />
onClick="location.href='http://www.webdeveloper.com/forum/archive/index.php/ea_games.jpeg';style.backgroundColor='#ff3300';"style="border:1px solid #000000;"><a href=http://www.webdeveloper.com/forum/archive/index.php/"ea_games.jpeg">hi hi hi</a></div><br />
</body><br />
</html><!--content-->Ok, but does not the following achieve the same result (extra div just to satisfy XHTML requirement for no inline elements in the body)<br />
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><br />
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"><br />
<head><br />
<title>Block Anchor</title><br />
<style type="text/css"><br />
a.block<br />
{ display: block;<br />
border:1px solid #000;<br />
background:#fff;<br />
}<br />
a.block:hover<br />
{ background:#ccc;<br />
}<br />
a.block:focus<br />
{ background:#f30;<br />
}<br />
<br />
</style><br />
</head><br />
<body><br />
<div><br />
<a class="block" href=http://www.webdeveloper.com/forum/archive/index.php/"ea_games.jpeg"> hi hi hi</a><br />
</div><br />
</body><br />
</html><!--content-->the one I wrote (I changed the doc type to the same as yours):<br />
<!-- m --><a class="postlink" href="http://quasi-ke.servebeer.com/test.html">http://quasi-ke.servebeer.com/test.html</a><!-- m --><br />
and the one you wrote:<br />
<!-- m --><a class="postlink" href="http://quasi-ke.servebeer.com/test2.html">http://quasi-ke.servebeer.com/test2.html</a><!-- m --><br />
At first I was suprised that hover was working in ie, but then I remember, oh yea, you are using a link. I just went with the java script approach because I figured if the browser does not support css the link will still be in a box... without the box the layout might get yucky... but then again if the layout is done in css it is going to break down into a unstyled form anyway.<!--content-->Originally posted by PeOfEo <br />
<snip />I just went with the java script approach because I figured if the browser does not support css the link will still be in a box... without the box the layout might get yucky. <br />
... err... if the browser does not support CSS (and the site is designed correctly) the layout does not matter anymore i.e. there will be no box... ;)<!--content-->I just edited my post with that right before you posted lol.<br />
Last edited by PeOfEo on 02-29-2004 at 04:41 PM<!--content-->LOL<!--content-->So what have we learned today? Two ways to acheive the same effect and either way seems to be accessable. Even though fred mentioned that using location.href could be considered a w3 standard violation since the block element would be inside of a link sort of....<!--content-->
 
Back
Top