-7

If I have CSS p:hover {background-color:yellow} like this... How to do EXACTLY THE SAME THING with jQuery? No, no a complex set of function calls and property dections, but how to say to jQuery to REUSE this universal browser build-in functionality?


Note after "FAST ATTACK" closing question: se @SamuelLiew's solution operating with 1 line jQuery code, at http://jsfiddle.net/uJXLG/1/


NOTES

This question is about REUSE (of browser build-in functionality), and the sample codes are only illustrations.

Why I need jQuery (instead "pure CSS")?

The use of "pure CSS" not permit to apply logic. Examples: 1) a event that activate and deactivate the CSS houver; 2) a complex selector logic.

Reinforce

Plase, somethig simple as

$('p:hover').css('color','red'); (that NOT RUNS)...

Not say to me to use

$('a').hover(function(){
          $(this).css('color', 'red');
     },function(){
         ...
     }
); 

that is very complex.


EDITED after @nevermind (see user's answer):

the code below is more complex (!!) than a single .addClass command, as sugested by @SamuelLiew. It is because SamuelLiew's code REUSED the browser's renderization behaviour.

var old_background; // to memorize original background

$.fn.hover_it = function(overcolor) { // TOO COMPLEX CODE AND OVERHEAD FOR DOM!!
    $(this).hover(function(){
        old_background = $(this).css("background-color");
        $(this).css("background-color",overcolor);
    },function(){
        $(this).css("background-color",old_background);
    });
}
 
$(document).ready(function() {
    $('p').hover_it('red');
});
Community
  • 1
  • 1
Peter Krauss
  • 13,174
  • 24
  • 167
  • 304

3 Answers3

1

They way you are thinking wont work. you have to use

jQuery(selector).hover

Otherwise just put it in css.

and its not that complicated the first function is for when you enter, the second is for when you leave. Just put the code you need to run in the right one.

If you just need to change the css style of the hovered element just use css.

Otherwise you have to use the .hover function

You could use action listeners but that would just as "complicated"

Patrick Evans
  • 41,991
  • 6
  • 74
  • 87
  • What is "action listeners" ? – xec Jun 05 '13 at 12:55
  • 1
    So, *event listeners*, just like .hover() **is** :) – xec Jun 05 '13 at 12:57
  • @xec -- yes, like hover. The "complex" function you don't want is going to be the easiest way. As Patrick said, the first function is called on mouse enter, the second on mouse leave. Not complicated :) – tymeJV Jun 05 '13 at 13:00
  • lol this wasnt xec's question... he was showing i was using the wrong "terminology" – Patrick Evans Jun 05 '13 at 13:06
  • @PatrickEvans see good solution (minial jQuery code!) at http://jsfiddle.net/uJXLG/1/ (2 comands of jQuery, adapted from S.Liew solution) and compare with incomplet and more complex nevermind code. – Peter Krauss Jun 06 '13 at 21:23
1

1) a event that activate and deactivate the CSS houver; 2) a complex selector logic.

In both examples mentioned above, you STILL CAN use CSS classes. Simply perform a toggleClass using jQuery for a specially-defined class that overrides the default hover functionality.

Example, you have this:

.myElement {
    color: green;
}
.myElement:hover {
    color: red;
}

Simply add this class:

.myElement.disabled {
    color: green;
}

Call .toggleClass('disabled') or .addClass('disabled') on any element that fails your logic or triggers your event.

See http://jsfiddle.net/samliew/NMmLN

Samuel Liew
  • 76,741
  • 107
  • 159
  • 260
  • Thanks a lot! YES, IT IS! I put the JQuery code (your .addClass indication) into http://jsfiddle.net/uJXLG/1/ to better illustration (please, you can add this link to your answer to show the focus of the question, and we can vote to repoen the question) – Peter Krauss Jun 06 '13 at 12:52
  • Your answer have important clues, but something important, that I added in http://jsfiddle.net/uJXLG/1/ was "`color: inherit; cursor: auto`", that is, way to "back to default" and not interfer in the original layout. – Peter Krauss Jun 06 '13 at 12:58
0

Here's a solution - I created a "complex" function and an example of how it's used:

$.fn.hover_it = function(over,out) { 
    $(this).hover(function(){
        $(this).css("background-color",over);
    },function(){
        $(this).css("background-color",out);
    });  
}

$(document).ready(function() {
    //usage
    $('p').hover_it('red','blue');
});
Joe
  • 15,205
  • 8
  • 49
  • 56
sinisake
  • 11,240
  • 2
  • 19
  • 27