Jquery gets child div in div

I have a css div and another div exists inside this div. How can I get a child div when the mouse hovers over the parent div? Like here:

<div class="parent">
  <div class="child">
  </div>
</div>

When the mouse goes through the parent div, I want the child div to be displayed.

+3
source share
4 answers

It works:

$('.child').hide();   // or $('.parent').children().hide();

$('.parent').hover(
    function() { $(this).children('div').show() },
    function() { $(this).children('div').hide() }
);

An example is http://jsfiddle.net/4kMuD/ , although identifiers are used for the selector, not classes.

I assumed (although you did not say) that you want the child div to disappear again when you no longer hang over the parent.

+7
source

Something like that?

  <div id="div1">
        div1
        <div id="div2" style="display:none;">div2</div>
    </div>

 $(function(){
        $("#div1").hover(function(){
            $("#div2").show();
        }, function(){
            $("#div2").hide();   
        });
    });
0
source
$('div.parent').bind('hover', 
   function(e) { $('div.child', this).show() },
   function(e) { $('div.child', this).hide() },
);
0
source
<html>
  <head>
    <title>Tests</title>
    <script src="jquery.js"></script>
    <script>
    $(document).ready(function(){
        $('#parent').mouseover(function() {
            $('#child').css('display', 'block');
        })
    });
    </script>
    <style>
        #parent {width: 100px; height: 100px; background-color: yellow;}
        #child { display:none; }
    </style>
  </head>
  <body>
        <div id="parent">
            <div id="child">Content</div>
        </div>
  </body>
</html>
0
source

All Articles