Does someone know if it is possible to grab the hover style of an element and then apply this style on click event so that the clicked style is the same as the hover style until a mouseout puts it back to its original style?
Hmmm ... isn't "active" supposed to be the state when the link is being clicked instead of the one after the link was clicked? I've tried this but it didn't work.
Anyway, what I've managed to do so far with javascript is capture the style onmouseover. So, I have a variable that contains "CSSStyleDeclaration". The problem is that I don't know how to apply this CSSStyleDeclaration to another element.
This is how you superimpose style overruling embedded stylesheet rules, in particular, the a:hover.
[tt]
<html>
<head>
<script language="javascript">
function doit(obj) {
var acss=document.styleSheets;
for(var i=0;i<acss.length;i++){
if (acss.cssRules) {
arules = acss.cssRules; //ff
} else if (acss.rules) {
arules = acss.rules; //ie
}
if (!arules) {return;} //do nothing
var scssStyleText="";
for (var j=0;j<arules.length;j++) {
if (arules[j].selectorText.toLowerCase()=="a:hover") { //ie A:active etc ff a:active etc
scssStyleText=arules[j].style.cssText;
break;
}
}
}
if (scssStyleText!="") {
obj.style.cssText=scssStyleText;
}
}
</script>
<style type="text/css">
/* arbitrary taken, say from ref [ignore]
As for the example you provided, my first concern is that my CSS files are very very big. So, I'm not sure it is a good idea to parse such big files for that purpose.
Isn't there a way to simply make use of CSSStyleDeclaration?
Meanwhile, I'v tried a different approach : create a specific class for this purpose and use document...className on click event. Works well but it produces unwanted changes when hover is called.
>...my first concern is that my CSS files are very very big.
It is very legitimate concern. I have forgotten at the last minute to revise by adding another break which may have a bearing on the speed.
[tt]
for(var i=0;i<acss.length;i++){
//etc etc
var scssStyleText="";
for (var j=0;j<arules.length;j++) {
if (arules[j].selectorText.toLowerCase()=="a:hover") { //ie A:active etc ff a:active etc
scssStyleText=arules[j].style.cssText;
break;
}
}
[blue]if (scssStyleText!="") {break;}[/blue]
}
[/tt]
But when we talk about speed, have to put into perspective of time scale on user-agent. Many lines above are not optimal such as string comparison with empty string rather than .length!=0 etc. Those are minor things. The main thing I had in mind is to establish the functionality and viability, else, I don't claim much.
>Isn't there a way to simply make use of CSSStyleDeclaration?
Together with the overall layout as I just used for illustration, I would think there should be some speedier referencing... only that these kinds of functionality are only sparsely used and the whole css referencing is still so fragile and full of proprietary/implementation specific that I cannot speak fluently enough. I would encourage you to explore the data model fuller to discover possible ways of more direct referencing.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.