473,387 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How do you swap foreground & background colors for a link (or any element)?

WITHOUT KNOWING ANYTHING ABOUT THE CURRENT COLORS, I want to swap the
foreground/background colors of a link when someone hovers over it. Is
this possible with HTML, CSS, DOM, & JavaScript? If so, how?

See my HTML below (or go to my test page at
http://fmbbowen.com:39353/misc/Misc.html). I also tried treating
..color as a number and subtracting the current color from 0xFFFFFF but
that did not work either. The numerous examples I looked at all set
the colors explicitly which is what I want to avoid.

Thanks!
=================== HTML below ===================
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html; charset=ISO-8859-1"
http-equiv="content-type">
<title></title>
<meta content="Franklin Bowen" name="author">

<script language="JavaScript" type="text/javascript">
<!--

function highlightLink(link)
{
// link.style.color = "#000000";
// link.style.backgroundColor = "#FFFFAA";
var swapColor = link.style.color
link.style.color = link.style.backgroundColor
link.style.backgroundColor = swapColor
link.style.fontWeight = "bolder";
}

function plainLink(link)
{
// link.style.color = "#0000FF";
// link.style.backgroundColor = "#FFFFFF";
var swapColor = link.style.color
link.style.color = link.style.backgroundColor
link.style.backgroundColor = swapColor
link.style.fontWeight = "normal";
}
-->
</script>

</head>
<body>
<H3>050920 Swap foreground & background colors when hover over
link</H3>
<a href="Contact.html" onmouseover="highlightLink(this)"
onmouseout="plainLink(this)">Contact</a>

<p>
<a href="http://validator.w3.org/check?uri=referer">
<img src="http://www.w3.org/Icons/valid-html401" alt="Valid HTML 4.01
Transitional" height="31" width="88">
</a>
</p>

</body>
</html>

Sep 20 '05 #1
4 4065
saz
In article <11**********************@z14g2000cwz.googlegroups .com>,
fr************@gmail.com says...
WITHOUT KNOWING ANYTHING ABOUT THE CURRENT COLORS, I want to swap the
foreground/background colors of a link when someone hovers over it. Is
this possible with HTML, CSS, DOM, & JavaScript? If so, how?

See my HTML below (or go to my test page at
http://fmbbowen.com:39353/misc/Misc.html). I also tried treating
.color as a number and subtracting the current color from 0xFFFFFF but
that did not work either. The numerous examples I looked at all set
the colors explicitly which is what I want to avoid.

Thanks!
=================== HTML below ===================
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html; charset=ISO-8859-1"
http-equiv="content-type">
<title></title>
<meta content="Franklin Bowen" name="author">

<script language="JavaScript" type="text/javascript">
<!--

function highlightLink(link)
{
// link.style.color = "#000000";
// link.style.backgroundColor = "#FFFFAA";
var swapColor = link.style.color
link.style.color = link.style.backgroundColor
link.style.backgroundColor = swapColor
link.style.fontWeight = "bolder";
}

function plainLink(link)
{
// link.style.color = "#0000FF";
// link.style.backgroundColor = "#FFFFFF";
var swapColor = link.style.color
link.style.color = link.style.backgroundColor
link.style.backgroundColor = swapColor
link.style.fontWeight = "normal";
}
-->
</script>

</head>
<body>
<H3>050920 Swap foreground & background colors when hover over
link</H3>
<a href="Contact.html" onmouseover="highlightLink(this)"
onmouseout="plainLink(this)">Contact</a>

<p>
<a href="http://validator.w3.org/check?uri=referer">
<img src="http://www.w3.org/Icons/valid-html401" alt="Valid HTML 4.01
Transitional" height="31" width="88">
</a>
</p>

</body>
</html>


Use CSS:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>

<meta content="text/html; charset=ISO-8859-1"
http-equiv="content-type">
<title></title>
<meta content="Franklin Bowen" name="author">
<style type="text/css">

a {
background-color: #ffffff;
color:#0000ff;
}

a:visited {
background-color: #ffffff;
color:#0000ff;
}

a:active {
background-color: #ffffaa;
color:#000000;
}

a:hover {
background-color: #ffffaa;
color:#000000;
}

</style>
</head>

<body>
<a href="Contact.html">Contact</a>
</body>

Sep 20 '05 #2

Fr******@Bowen.net wrote:
WITHOUT KNOWING ANYTHING ABOUT THE CURRENT COLORS, I want to swap the
foreground/background colors of a link when someone hovers over it.


Impossible in CSS, easy in JS

Remember that "colours" aren't an integer type, they're three integers
packed into sub-fields within that large integer. If you're swapping
them or XORing them then this doesn't matter, but if you're doing an
operation like subtraction where there's a risk of carrying between
bits (and thus between RGB channels) then you need to split the colour
value into RGB components first, subtract each one individually and
then put them back together.

Sep 21 '05 #3
saz wrote:
In article <11**********************@z14g2000cwz.googlegroups .com>,
fr************@gmail.com says...
WITHOUT KNOWING ANYTHING ABOUT THE CURRENT COLORS, I want to swap the
foreground/background colors of a link when someone hovers over it.
Use CSS:

[snip] a {
background-color: #ffffff;
color:#0000ff;
}

a:visited {
background-color: #ffffff;
color:#0000ff;
}

a:active {
background-color: #ffffaa;
color:#000000;
}

a:hover {
background-color: #ffffaa;
color:#000000;
}


Try as I might, I don't see how this *swaps* the foreground and
background colors as the OP asked, nor how this amounts to not knowing
what the current colors are, as he specified.
Sep 21 '05 #4
saz
In article <3p************@individual.net>,
hm*******************@comcast.net says...
saz wrote:
In article <11**********************@z14g2000cwz.googlegroups .com>,
fr************@gmail.com says...
WITHOUT KNOWING ANYTHING ABOUT THE CURRENT COLORS, I want to swap the
foreground/background colors of a link when someone hovers over it.

>
Use CSS:

[snip]
a {
background-color: #ffffff;
color:#0000ff;
}

a:visited {
background-color: #ffffff;
color:#0000ff;
}

a:active {
background-color: #ffffaa;
color:#000000;
}

a:hover {
background-color: #ffffaa;
color:#000000;
}


Try as I might, I don't see how this *swaps* the foreground and
background colors as the OP asked, nor how this amounts to not knowing
what the current colors are, as he specified.


I ignored the bold line because he was setting colors in his code. To
me that meant that he DID have colors in mind.

Many posters in this group contradict themselves, I thought he was one
of them.
Sep 21 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Bill Parker | last post by:
Hi Is there a way to generate colour hex strings that are guaranteed to be dark and thus good as random-generated text colours? something like -------------------...
4
by: Scott | last post by:
There is a setting in IE for "Print background colors and images." I've noticed that without checking this option, a web page will print CSS lines between table rows, but a background color of...
3
by: Felix Natter | last post by:
hi, is there a way (html/css/javascript) to force IE to print background-colors exactly as specified in html/stylesheets? I tried to use @media print { ... } to override IE's "print background...
3
by: Bob - Andover, MA | last post by:
I know how to define a background color for the entire page, and for a table, and even using style sheets and 'P CLASS=...' for changing paragraph background colors. However, I'd like to be able...
5
by: Lee K. Seitz | last post by:
I have a set of pages with a special stylesheet used to override some styles for printing. One of the things I've tried to do is reverse the colors of my navigation. On the screen, I have a black...
10
by: wideopensvt | last post by:
I'm having a display issue with Javascript pages on a new computer. Background colors don't appear within tables or frames in Internet Explorer 6. Example #1:...
16
by: J. B. Moreno | last post by:
I read the faq, and it mentions that IE 4 on windows requires setting a printing option to allow background colors to be printed. Things change, life goes on, other browsers come into...
2
by: gnosys | last post by:
In ASP.Net 1.1 using C#, I'm trying to dynamically change the background colors of certain listbox items based on some criteria. For example:
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.