专家css选择器请在里面

时间:2018-03-26 03:42:13

标签: javascript html css

您好我需要创建一个多维下拉菜单。但我的悬停选择不起作用。我不知道如何使用其他类div父级的选择器到其他类div父级。看看风格的代码。我想将hvcontainer设置为在悬停到lvcontainer时显示块。欢迎使用Javascript解决方案



        body {
          margin: auto;
        }
        
        #container {
          display: table;
        }
        
        #lcontainer {
          padding: 0 10px 0 10px;
          display: table-cell;
        }
        
        #rcontainer {
          padding: 0 10px 0 10px;
          display: table-cell;
        }
        
        .rvcontainer {
          display: none;
        }
    
        #lcontainer ~ .lvcontainer:hover + #rcontainer ~ .rvcontainer {
          display: block;
        }

    <div id="container">
      <div id="lcontainer">
        <div class="lvcontainer">
          Country
        </div>
        <div class="lvcontainer">
          Genre
        </div>
      </div>
      <div id="rcontainer">
        <div class="rvcontainer">
          Japan
          <br> Korea
          <br> American
          <br>
        </div>
        <div class="rvcontainer">
          Comedy
          <br> Mystery
          <br> Horror
          <br>
        </div>
      </div>
    </div>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:0)

首先你的CSS选择器是错误的。它甚至没有选择正确悬停的元素。它试图选择它作为兄弟而不是孩子。

其次,使用纯CSS可能无法做到这一点。因为要实现这一点,您必须选择元素的父元素,然后选择父元素的兄弟元素。

JAVASCRIPT:您必须在.lvcontainer上监听“鼠标悬停”事件,然后将.rvcontainer的显示更改为阻止。然后在mouseleave事件中,将其设置回以前的状态。

答案 1 :(得分:0)

您可以使用Bootstrap实现此目的:

$(document).ready(function(){
  $('.dropdown-submenu a.test').on("click", function(e){
    $(this).next('ul').toggle();
    e.stopPropagation();
    e.preventDefault();
  });
});
.dropdown-submenu {
    position: relative;
}

.dropdown-submenu .dropdown-menu {
    top: 0;
    left: 100%;
    margin-top: -1px;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

<div class="container">
  <h2>Multi-Level Dropdowns</h2>
  <p>In this example, we have created a .dropdown-submenu class for multi-level dropdowns (see style section above).</p>
  <p>Note that we have added jQuery to open the multi-level dropdown on click (see script section below).</p>                                        
  <div class="dropdown">
    <button class="btn btn-default dropdown-toggle" type="button" data-toggle="dropdown">Tutorials
    <span class="caret"></span></button>
    <ul class="dropdown-menu">
      <li><a tabindex="-1" href="#">HTML</a></li>
      <li><a tabindex="-1" href="#">CSS</a></li>
      <li class="dropdown-submenu">
        <a class="test" tabindex="-1" href="#">New dropdown <span class="caret"></span></a>
        <ul class="dropdown-menu">
          <li><a tabindex="-1" href="#">2nd level dropdown</a></li>
          <li><a tabindex="-1" href="#">2nd level dropdown</a></li>
          <li class="dropdown-submenu">
            <a class="test" href="#">Another dropdown <span class="caret"></span></a>
            <ul class="dropdown-menu">
              <li><a href="#">3rd level dropdown</a></li>
              <li><a href="#">3rd level dropdown</a></li>
            </ul>
          </li>
        </ul>
      </li>
    </ul>
  </div>
</div>

示例来自W3Schools