【问题标题】:How to show the child div on mouse hover of parent div如何在父div的鼠标悬停时显示子div
【发布时间】:2013-05-04 23:36:22
【问题描述】:

我有许多父 div (.parent_div),每个都包含一个子 div (.hotqcontent),我使用循环从数据库中输出数据。

以下是我当前的标记:

<div class="content">
  <div class="parent_div">
    <div class="hotqcontent">
      content of first div goes here...
    </div>
    <hr>
  </div>
  <div class="parent_div">
    <div class="hotqcontent">
      content of second div goes here...
    </div>
    <hr>
  </div>
  <div class="parent_div">
    <div class="hotqcontent">
      content of third div goes here...
    </div>
    <hr>
  </div>
  <div class="parent_div">
    <div class="hotqcontent">
      content of fourth div goes here...
    </div>
    <hr>
  </div>
</div>

我想要实现的是,当用户将鼠标悬停/鼠标悬停在父 div 上时,应该显示其中包含的子 div 的内容。

为了实现这一点,我编写了以下 jQuery 脚本,但它似乎无法正常工作。它甚至没有显示警报!

<script> 
$(document).ready(function() {
  $(function() {
    $('.parent_div').hover(function() {
      alert("hello");
      $('.hotqcontent').toggle();
    });
  });
}); 
</script>

如何修改或替换现有代码以实现所需的输出?

【问题讨论】:

    标签: javascript jquery css css-selectors


    【解决方案1】:

    如果你想要纯 CSS,那么你可以这样做......

    在下面的 CSS 中,在初始化/页面加载时,我使用 display: none; 隐藏子元素,然后在父元素的 hover 上隐藏子元素,比如说有一个 class parent_div,我使用 display: block; 来取消隐藏元素。

    .hotqcontent {
       display: none;
       /* I assume you'll need display: none; on initialization */ 
    }
    
    .parent_div:hover .hotqcontent { 
       /* This selector will apply styles to hotqcontent when parent_div will be hovered */
       display: block;
       /* Other styles goes here */
    }
    

    Demo

    【讨论】:

    • 这可能是更好的方法,因为它是一个表示问题。此外,这在设计上不易受到 DOM 状态等问题的影响。
    • 我测试过,但它没有在鼠标悬停在父 div 上时显示 hotqcontent div。为什么?请帮忙
    • 你是一个非常乐于助人和善良的人。作为一个人,这是非常重要的。我认为阅读此评论的人也会同样帮助其他开发人员。
    【解决方案2】:

    试试这个

    $(document).ready(function() {
      $('.parent_div').hover(function() { 
        alert("hello");
        $(this).find('.hotqcontent').toggle(); 
      });
    });
    

    或者

    $(function() {
      $('.parent_div').hover(function() { 
        alert("hello");
        $(this).find('.hotqcontent').toggle(); 
      });
    });
    

    【讨论】:

      【解决方案3】:

      您可以为此使用 css,

      .parent_div:hover .hotqcontent {display:block;}
      

      【讨论】:

        【解决方案4】:

        这可以用纯 css 来完成(我添加了一些位,只是为了让 JSFIDDLE 更整洁):

        .parent_div {
            height: 50px;
            background-color:#ff0000;
            margin-top: 10px;
        }
        
        .parent_div .hotqcontent {
            display: none;
        }
        
        .parent_div:hover .hotqcontent {
            display:block;
        }
        

        如果用户禁用了 Javascript,这将确保您的网站仍以相同的方式运行。

        演示: http://jsfiddle.net/jezzipin/LDchj/

        【讨论】:

          【解决方案5】:

          使用.hotqcontent,您将选择该类的每个元素。但是您只想选择父元素下方的.hotqcontent 元素。

          $('.hotqcontent', this).toggle();
          

          【讨论】:

            【解决方案6】:
            $(document).ready(function(){
                $('.parent_div').on('mouseover',function(){
                    $(this).children('.hotqcontent').show();
                }).on('mouseout',function(){
                    $(this).children('.hotqcontent').hide();
                });;
            });
            

            JSFIDDLE

            【讨论】:

              【解决方案7】:

              document.ready 内部不需要 document.ready 函数..

              试试这个

                $(function() {  //<--this is shorthand for document.ready
                      $('.parent_div').hover(function() { 
                           alert("hello");
                           $(this).find('.hotqcontent').toggle(); 
                           //or
                          $(this).children().toggle(); 
                     });
                  });
              

              是的,您的代码将使用hotqcontent..(我认为您不需要这个)类切换所有div,如果您想切换特定的div,然后使用this 引用来切换它特定的 div

              更新

              您可以在委托事件上使用动态生成的元素

              $(function() {  //<--this is shorthand for document.ready
                      $('.content').on('mouseenter','.parent_div',function() { 
                           alert("hello");
                           $(this).find('.hotqcontent').toggle(); 
                           //or
                          $(this).children().toggle(); 
                     });
                  });
              

              【讨论】:

              【解决方案8】:

              你可以试试这个:

              jQuery(document).ready(function() {
                jQuery("div.hotqcontent").css('display','none');
                jQuery("div.parent_div").each(function(){
                  jQuery(this).hover(function(){
                      jQuery(this).children("div.hotqcontent").show(200);
                  }, function(){
                      jQuery(this).children("div.hotqcontent").hide(200);
                  });
                });
              });
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2023-04-10
                • 1970-01-01
                相关资源
                最近更新 更多