473,732 Members | 2,219 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

accesing to the hover style with javascript

Hi.

I need to access to the hover style of an element with javascript. How
can i do this?

Best regards
Manuel

Mar 16 '07 #1
3 11594
<mf*****@gmail. comwrote:
I need to access to the hover style of an element with javascript. How
can i do this?
u can bothe add a css rule of modify an existing one.

suppose the declared css is :

a.myClass:hover {<the directives}

in that case u want to modify an existing style.

first u'll have to loop over all the styles nodes, using DOM, in order
to find the selector text "a.myClass:hove r ", we will call it by a
variable name "selector" and the new rules "newRules" then :

var rgs=new RegExp('\\s*'+s elector+"\\s*", "g")
var css=document.st yleSheets;
for(var k=0;k< css.length;k++) {
var rules=document. styleSheets[k].cssRules;
for(var i=0;i<rules.len gth;i++){
if(rgs.test(rul es[i].selectorText)) {
rules[i].style.cssText= newRules;
}
}
}
with, for example :

selector="a.myC lass:hover";
newRules="paddi ng:0 15px 0 1em; background:#fce fab;";

or, u might want to change only one property for example :

property="backg round";
value="blue";

in that case i'll have to split the cssText of the rule corresonding to
the selector like :

var cssTexts=rules[i].style.cssText. split(';');
then loop over the properties to change the "background :

for(var j=0;j<cssTexts. length;j++){
if(rgp.test(css Texts[j])) cssTexts[j]=property+':'+v alue;
}

using another Regexp :
var rgp=new RegExp('^\\s*'+ property+"\\s*: ","g")

then restore the whole cssText by :
rules[i].style.cssText= cssTexts.join(' ;');

that's all if u want to change a property for a given selector.

It is much more easier to *add* a new rule if the rule corresponding to
the selector doesn't exists :

saying we have :
var aRule='h1, h3 {color:red;back ground:#666;}';
suppose, worst case, we don't have *any* <style ... /in the document
head then :

var head=document.g etElementsByTag Name('head')[0];
var style=document. createElement(' style');
style.type='tex t/css';
head.appendChil d(style);
var aRule='h1, h3 {color:red;back ground:#666;}';
document.styleS heets[0].insertRule(aRu le,0);

otherwise, simplest case, their is allready some styles nodes in your
document, then :

var stylesLen=docum ent.getElements ByTagName('styl e').length;
var
rulesLen=docume nt.getElementsB yTagName('style ')[stylesLen-1].cssRules.le
ngth;
var aRule='h1, h3 {color:red;back ground:#666;}';
document.styleS heets[stylesLen-1].insertRule(aRu le,rulesLen);
Mar 16 '07 #2
On 16 mar, 20:02, unbewusst.s...@ wortanschahung. com.invalid (Une
Bévue) wrote:
<mfc1...@gmail. comwrote:
I need to access to the hover style of an element with javascript. How
can i do this?

u can bothe add a css rule of modify an existing one.

suppose the declared css is :

a.myClass:hover {<the directives}

in that case u want to modify an existing style.

first u'll have to loop over all the styles nodes, using DOM, in order
to find the selector text "a.myClass:hove r ", we will call it by a
variable name "selector" and the new rules "newRules" then :

var rgs=new RegExp('\\s*'+s elector+"\\s*", "g")
var css=document.st yleSheets;
for(var k=0;k< css.length;k++) {
var rules=document. styleSheets[k].cssRules;
for(var i=0;i<rules.len gth;i++){
if(rgs.test(rul es[i].selectorText)) {
rules[i].style.cssText= newRules;
}
}
}

with, for example :

selector="a.myC lass:hover";
newRules="paddi ng:0 15px 0 1em; background:#fce fab;";

or, u might want to change only one property for example :

property="backg round";
value="blue";

in that case i'll have to split the cssText of the rule corresonding to
the selector like :

var cssTexts=rules[i].style.cssText. split(';');
then loop over the properties to change the "background :

for(var j=0;j<cssTexts. length;j++){
if(rgp.test(css Texts[j])) cssTexts[j]=property+':'+v alue;
}

using another Regexp :
var rgp=new RegExp('^\\s*'+ property+"\\s*: ","g")

then restore the whole cssText by :
rules[i].style.cssText= cssTexts.join(' ;');

that's all if u want to change a property for a given selector.

It is much more easier to *add* a new rule if the rule corresponding to
the selector doesn't exists :

saying we have :
var aRule='h1, h3 {color:red;back ground:#666;}';
suppose, worst case, we don't have *any* <style ... /in the document
head then :

var head=document.g etElementsByTag Name('head')[0];
var style=document. createElement(' style');
style.type='tex t/css';
head.appendChil d(style);
var aRule='h1, h3 {color:red;back ground:#666;}';
document.styleS heets[0].insertRule(aRu le,0);

otherwise, simplest case, their is allready some styles nodes in your
document, then :

var stylesLen=docum ent.getElements ByTagName('styl e').length;
var
rulesLen=docume nt.getElementsB yTagName('style ')[stylesLen-1].cssRules.le
ngth;
var aRule='h1, h3 {color:red;back ground:#666;}';
document.styleS heets[stylesLen-1].insertRule(aRu le,rulesLen);
oh, thank you very much.

that was what needed

Mar 16 '07 #3
ASM
mf*****@gmail.c om a écrit :
Hi.

I need to access to the hover style of an element with javascript. How
can i do this?
Not clear ...
An example ?
--
Stephane Moriaux et son (moins) vieux Mac déjà dépassé
Stephane Moriaux and his (less) old Mac already out of date
Mar 17 '07 #4

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

Similar topics

10
3731
by: Cezar | last post by:
Hello. How can simple emulating in IE this class ? ..example:hover{ border: 1px solid #696969; } Greets, Cezar.
1
5423
by: Jon W | last post by:
This is a small table with hover on the table cells. The first cell is setup to switch from div element to input element by use of display:block/none. In IE, onclick the input element is displayed correctly but it disappears if another hover is triggered. Any thoughts? Thanks,Jonny <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN">
2
11266
by: Sugapablo | last post by:
Can anyone help me out with some code to change three table cells (<td>) when one is hovered over? I have a calendar grid where each day is made up of three table cells and I want all three to change color when any of the three are hovered over. I would prefer to do this with pure CSS if possible, but if Javascript is the only way...so be it. --
2
3075
by: Elisa | last post by:
I have two images, each with defined CSS hover states. I would like a mouseover on one image to trigger the hover state of the other image. Is this possible using javascript? If so, how? Any help would be appreciated. Thanks, CJ
2
4169
by: skipc | last post by:
I posted a recent message regarding navigating a table using arrow keys. I've got the code for the navigation working. My table contains rows of links (anchors). I can get to a specific link using the code for capturing a key. However, once I get to the link, I can focus(), for example... but what I really want is to activate the "hover" styles. I can fake the hover effect using javascript, but how do I do this and still allow the...
7
77373
by: Matt | last post by:
Is there a way to increase the amount of time a hyperlink title displays or show the desired text using javascript? <a title="This is the title text displayed on hover.">Displayed Text</a> The above HTML will display the title text when the mouse is hovered over the displayed text but it disappears quickly. There may be a better way to display text on hover using javascript but
4
19453
by: bne | last post by:
Hi, My brain's a bit fried on this one. I'm using the li:hover method to display sub menus at http://dev.hyl.co.uk/guide4life/. All works swimmingly in FF, however IE7 loses the hover (and so the sub menu) when you mouse over items further down the sub menu list. This only seems to happen when the sub menu is over the text of a paragraph (try further down the page eg: "Confidentiality" - it works!?)
3
2584
by: noize | last post by:
I have found other bugs related to using hover is CSS with IE, but I cannot find a fix for my issue. I have checked it in both IE 6 & 7 with the same results. I have built a drop-down menu using CSS and a touch of Javascript, by combining a couple tutorials I found online. The bug is when you mouse-over the menu in IE, the sub-menu pops out, but will disappear when you mouse-over it, but it doesn't happen everytime. Sometimes, it works...
4
2262
by: Sigilaea | last post by:
My previous post got squashed because m post is too long. Sorry for that: I have an AJAX page with a CSS menu at the top. My problem is the hover functionality stops working after someone clicks on one of the details in the main blue section. This only occurs in IE6. IE7 and FF work correctly. The URL to the page is: http://www.coralap.com/dies281css.aspx Steps to reproduce: 1. Mouse over the top menu and verify the hover works. 2....
0
8946
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9447
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9235
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9181
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6031
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4809
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2721
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2180
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.