Show child div on mouse hover of parent - needs javascript?

前端 未结 7 1993
悲哀的现实
悲哀的现实 2021-02-01 13:27

I need to show a div when you house over its parent (default is for the child div to be hidden). Is the only way to do this with javascript? Thanks

相关标签:
7条回答
  • 2021-02-01 13:40

    No JS required.

    .hidden {
      display: none;
    }
    
    .parent:hover .hidden {
      display: block;
    }
    <div class="parent">
      <p>Hello, I'm a child...</p>
      <p class="hidden">..and so am I but I'm hidden.</p>
    </div>

    0 讨论(0)
  • 2021-02-01 13:47

    @jdln; yes

    css:

    .outer {
        background:red;
        height:100px;
    }
    .box1 {
        background:blue;
        height:100px;
        width:80px;
        display:none;
    }
    .outer:hover .box1{
        display:block;
        cursor:pointer;
    }
    

    check the fiddle: http://jsfiddle.net/sandeep/XLTV3/1/

    0 讨论(0)
  • 2021-02-01 13:48

    I initially was using css solution above, but had to use jQuery because my child div contained an image which caused hover to flicker. Here we're displaying child when hovering over parent (if desktop screen size) on mouseenter and hiding it on mouseleave but only if now hovering over main page container, to minimize flickering:

    $(document).ready(function () {
        $(".parent-qtip").mouseenter(function () {
            if ($(window).width()>=1200)
                $(this).children(".child-qtip").show();
        });
        $(".parent-qtip").mouseleave(function () {
            if ($('.page-content').find('.container:hover').length)
                $('.child-qtip').hide();
        });
    });
    
    0 讨论(0)
  • 2021-02-01 13:52

    Since it's a child element you don't need javascript. Here is an example.

    0 讨论(0)
  • 2021-02-01 13:54

    With jQuery, absolutely:

    $("#some_parent").hover(function ()
    {
        $(this).children("#some_child_div").show();
    });
    
    0 讨论(0)
  • 2021-02-01 13:55

    Using jQuery you can add a mouseover event to the parent div, and show the child div. See this fiddle as an example.

    0 讨论(0)
提交回复
热议问题