views:

128

answers:

3

Code as follows:

<div class="mama">
    <div class="son">Item 1</div>
</div>
<div class="mama">
    <div class="son">Item 2</div>
</div>

$(".mama").hover(
    function() {
     $(".son").show();
    },
    function() {
     $(".son").hide();
    }
);

Forward to help. Thanks!

+1  A: 

If you're asking how to only hide the ".son" blocks inside each ".mama" block, then it'd be something like this:

$('.mama').each(function() {
  var mama = $(this);
  mama.hover(
    function() { mama.find('.son').show(); },
    function() { mama.find('.son').hide(); }
  );
});
Pointy
There's no need for the added complexity of this. See my answer.
cletus
If we do like this: ... $('.mama') ...is only the first block is in effect. We have to do like this:... $("div.mama") ...Thank you !
Tran Tuan Anh
+1  A: 

if all you're doing is showing or hiding content, you don't need jQuery. CSS already has this functionality

 .mama .son {
      display:none;
 }

 .mama:hover .son {
      display:block;
 }
Jonathan Fingland
IE6 doesn't support :hover on non-anchors.
cletus
I just give a simple example to other complex problems using JQuery. Thanks!
Tran Tuan Anh
A: 

Use the hover() event and just scope your actions to the relevant elements:

$("div.mama").hover(function() {
  $(this).find("div.son").show();
}, function() {
  $(this).find("div.son").hide();
});

There are many variations on how you can limit this to only children of the affected element.

cletus
How this very positive and easy to understand. Thank you very much!
Tran Tuan Anh