views:

621

answers:

5

I’m trying to prevent the browser from using the :hover effect of the CSS, via JavaScript.

I have set the a and a:hover styles in my CSS, because I want a hover effect, if JS isn’t available. But if JS is available, I want to overwrite my CSS hover effect with a smoother one (using the jQuery color plugin for example.)

I tried this:

$("ul#mainFilter a").hover(
     function(o){ o.preventDefault(); ...do my stuff... }, 
     function(o){ o.preventDefault(); ...do my stuff... });

I also tried it with return false;, but it does not work.

Here is an example of my problem: http://jsfiddle.net/4rEzz/. The link should just fade without getting gray.

As mentioned by fudgey, a workaround would be to reset the hover styles using .css() but I would have to overwrite every single property, specified in the CSS (see here: http://jsfiddle.net/raPeX/1/ ). I am looking for a generic solution.

Does anyone know how to do this?

PS: I do not want to overwrite every style i have set for the hover.

+4  A: 

I would use CSS to prevent the :hover event from changing the appearance of the link.

a{
  font:normal 12px/15px arial,verdana,sans-serif;
  color:#000;
  text-decoration:none;
}

This simple CSS means that the links will always be black and not underlined. I cannot tell from the question whether the change in the appearance is the only thing you want to control.

Christopher Altman
its good that there is a :hover effect if JS is not avalible on a client. But if it is i need to overwrite it. I have set a:hover class in my css, is just want to disable it.
meo
@meo: You can't disable the CSS psuedo classes, but you can override it by setting all the link styles to have the same appearance/parameters. Both this answer and my answer do this, just in different ways.
fudgey
+1  A: 

Try just setting the link color:

$("ul#mainFilter a").css('color','#000');

Edit: or better yet, use the CSS, as Christopher suggested

fudgey
http://jsfiddle.net/raPeX/ i have made an example. It works but its kind of stupid to do that. i would have to retrieve every CSS value and add them as style? There must be a better way. I don't want to do that. but +1 for the hint
meo
Hi Meo, no no, my code above was targeting a specific link as that is what I thought you wanted. You could generalize it more by just using `a` instead of `ul#mainFilter a` if that is what you mean, or are you saying every link on the page is a different color?
fudgey
I've updated your demo, with comments, to show you what I mean: http://jsfiddle.net/raPeX/2/
fudgey
ive got that already thank you. But the problem is, there is not just the color changing on my hover, sometimes its the background to, the border, sometimes even the line-height. Thats why it would be easier for me to just prevent the hover, den to overwrite every single hover effect.
meo
Partial patch for this idea: You could create css a rule that selects for `a.jsOverride:hover` that overrides all of the css characteristics, so the js would just have to add that `jsOverride` class to all the links when the page loads...
grossvogel
+7  A: 

You can manipulate the stylesheets and stylesheet rules themselves with javascript

var sheetCount = document.styleSheets.length;
var lastSheet = document.styleSheets[sheetCount-1];
var ruleCount;
if (lastSheet.cssRules) { // Firefox uses 'cssRules'
    ruleCount = lastSheet.cssRules.length;
}
else if (lastSheet.rules) { / /IE uses 'rules'
    ruleCount = lastSheet.rules.length;
}
var newRule = "a:hover { text-decoration: none !important; color: #000 !important; }";
// insert as the last rule in the last sheet so it
// overrides (not overwrites) previous definitions
lastSheet.insertRule(newRule, ruleCount);

Making the attributes !important and making this the very last CSS definition should override any previous definition, unless one is more specifically targeted. You may have to insert more rules in that case.

Stephen P
Or you could put all the noscript rules in one stylesheet and then disable the stylesheet from javascript.
Sean Hogan
Or put your noscript stylesheet `<link>` tag inside a `<noscript>` element? That’d work, right?
Paul D. Waite
Or delete the "offending" rule: https://developer.mozilla.org/en/DOM/stylesheet.deleteRule
RoToRa
@RoToRa: can this be done in every major browser?
meo
Even IE5 can do it. It's method is called differently, but it should work: http://msdn.microsoft.com/en-us/library/ms531195%28v=vs.85%29.aspx . I haven't checked other browsers, but I would guess all modern browsers implement the standard.
RoToRa
+20  A: 

There isn’t a pure JavaScript generic solution, I’m afraid. JavaScript isn’t able to turn off the CSS :hover state itself.

You could try the following alternative workaround though. If you don’t mind mucking about in your HTML and CSS a little bit, it saves you having to reset every CSS property manually via JavaScript.

HTML

<body class="nojQuery">

CSS

/* Limit the hover styles in your CSS so that they only apply when the nojQuery 
class is present */

body.nojQuery ul#mainFilter a:hover {
    /* CSS-only hover styles go here */
}

JavaScript

// When jQuery kicks in, remove the nojQuery class from the <body> element, thus
// making the CSS hover styles disappear.

$(function(){}
    $('body').removeClass('nojQuery');
)
Paul D. Waite
i like your thinking outside the box
knittl
I like this solution better than the noscript solutions because this works when the CSS is entirely contained in external files, and it also works during page load, if the CSS loads first but the JS doesn't load until later. Using jQuery to "progressively enhance" things often results in pages that don't work well if you click on stuff before it's finished loading. Even with a fast browser on a fast link I see this a lot.
Mr. Shiny and New
@Mr. Shiny and New: I agree, I like this better than my solution also! I'm not sure why I didn't notice the elegance of this one before...
Josh
“I like this solution better than the noscript solutions because this works when the CSS is entirely contained in external files” — so does the `<noscript>` solution. You just put `<link>` tags inside your `<noscript>`, instead of `<style>` tags. My solution does allow you to keep everything inside one stylesheet file rather than multiple files though.
Paul D. Waite
its not exactly what i was looking for but i think this solution is the best compromise! Thx to all of you
meo
+4  A: 

This is similar to aSeptik's answer, but what about this approach? Wrap the CSS code which you want to disable using JavaScript in <noscript> tags. That way if javaScript is off, the CSS :hover will be used, otherwise the JavaScript effect will be used.

Example:

<noscript>
<style type="text/css">
ul#mainFilter a:hover {
  /* some CSS attributes here */
}
</style>
</noscript>
<script type="text/javascript">
$("ul#mainFilter a").hover(
     function(o){ /* ...do your stuff... */ }, 
     function(o){ /* ...do your stuff... */ });
</script>
Josh
I like this one: simpler.
Paul D. Waite