代码之家  ›  专栏  ›  技术社区  ›  ZTTR

下拉列表无效html css

  •  0
  • ZTTR  · 技术社区  · 9 年前

    我已经创建了这个HTML页面,但是 :hover #drop 不起作用。

    #drop {
        display: inline-block;
        color: white;
        background-color: #4CAF50;
        padding: 10px;
    }
    #droplist {
        display: none;
        position: absolute;
        z-index: 1;
    }
    #droplist a {
        display: block;
        text-decoration: none;
        color: white;
        background-color: olivedrab;
        padding: 10px;
    }
    #drop:hover #droplist {
        display: block;
    }
    #droplist a:hover {
        background-color: olive;
    }
    <!DOCTYPE html>
    <html lang="it">
    <!--   ...   -->
    <body>
        <div id="pagina">
            <div id="drop">Hover for open the menu</div>
            <div id="droplist">
                <a href="#a">Link 1</a>
                <a href="#b">Link 2</a>
                <a href="#c">Link 3</a>
            </div>
            <br/>text text text text text text text text text
        </div>
    </body>
    </html>

    但是元素 #droplist 没有出现。

    1 回复  |  直到 9 年前
        1
  •  0
  •   Ivan Jeffrey Zhao    9 年前

    您无法选择 #drop:hover #droplist 因为 #droplist 不是的孩子 #drop .

    使用 #drop:hover + #droplist 相反

    这个 element1 + element2 选择器用于选择和设置每个 element2 之后立即放置 element1

    #drop {
      display: inline-block;
      color: white;
      background-color: #4CAF50;
      padding: 10px;
    }
    
    #droplist {
      display: none;
      position: absolute;
      z-index: 1;
    }
    
    #droplist a {
      display: block;
      text-decoration: none;
      color: white;
      background-color: olivedrab;
      padding: 10px;
    }
    
    #drop:hover+#droplist {
      display: block;
    }
    
    #droplist a:hover {
      background-color: olive;
    }
    
    #droplist:hover {
      display: block
    }
    <!DOCTYPE html>
    <html lang="it">
    <!--   ...   -->
    
    <body>
      <div id="pagina">
        <div id="drop">Hover for open the menu</div>
        <div id="droplist">
          <a href="#a">Link 1</a>
          <a href="#b">Link 2</a>
          <a href="#c">Link 3</a>
        </div>
      </div>
    </body>
    
    </html>

    :您可能需要加载项

    #droplist:hover {
       display: block
    }
    

    这样,当你悬停下拉菜单时,它不会消失