【问题标题】:Why is the element displayed on hover not showing in nested CSS drop downs?为什么悬停时显示的元素没有显示在嵌套的 CSS 下拉列表中?
【发布时间】:2017-11-20 15:53:22
【问题描述】:

我正在尝试让嵌套的 CSS 下拉菜单工作。菜单中的信息是使用数据库中的结果生成的。第一个下拉菜单效果很好;只是当我将鼠标悬停在行上时,什么都没有发生。

谁能看出我哪里出错了?我的理解是,当我将鼠标悬停在类名为 test 的表格行(即&lt;tr&gt;)上时,它应该在右侧显示类名为 dropcontent2 的元素这个,但它不起作用><.>

下面是我目前得到的图像 - 我试图显示在我悬停在行的右侧的“测试”文本,但是即使 显示 它也只是显示属性设置为 none

启动 PHP:

if (!empty($result))
  {
            echo "<div class='dropdown'>";
              echo "<span class='label label-success'>Domain Found In History</span>";
                echo " <div class='dropdown-content'>";
                    $this_database->displaySearchResults($result);
                  echo "</div>";
              echo "</div>";
  }

上面调用的PHP函数:

  function displaySearchResults($idArray)
{
  $servername = $this->servername;
  $username = $this->username;
  $password = $this->password;
  $dbname = $this->dbname;
  #Opens the MYSQL Connection
  $conn = mysqli_connect($servername, $username, $password, $dbname);
    if (!$conn)
    {
      die("Connection failed: " . mysqli_connect_error());
    }
    ## Begining of formatting of drop down table
    echo "<table class='table table-condensed'>";
     echo "<tr>";
      echo "<th>Date</th>";
        echo "<th>Time</th>";
          echo "</tr>";
          ##Loops through all the matching ID's
    for ($i = 0; $i < count($idArray); $i++)
    {
        $stmt = $conn->prepare("SELECT * FROM dns WHERE id LIKE '$idArray[$i]' ");
          $stmt->bind_param($idArray[$i]);
            $stmt->execute();
              $result = $stmt->get_result();
              ## For ever matching ID it prints out the date/time into the drop down table
              while($row=$result->fetch_assoc())
              {
                echo "<tr class='test'>";
                  echo "<td>".$row["date"]."</td>";
                    echo "<td>".$row["time"]."</td>";
                        echo "</tr>";
                      echo "<div class='drop2content'>";
                        echo "<tr>";
                          echo "<td>Test </td>";
                              echo "</tr>";
                                echo "</div>";

              }
  }
              echo "</table>";
}

CSS

 /* Show History Drop down Styling */

/* The container <div> - needed to position the dropdown content */
.dropdown
{
   position: relative;
   display: inline-block;
}

/* Dropdown Content (Hidden by Default) */
.dropdown-content
{
   display: none;
   position: absolute;
   z-index: 1;
   margin-top:2%;
   background-color:white;
}

/* Show the dropdown menu on hover */
.dropdown:hover .dropdown-content
{
   display: block;
}

.drop2content
{
  display:none;
  position:absolute;
  background-color:white;
}

.test:hover .drop2content
{
  display:block;
  position:absolute;
  left:100%;
  top:0%;

}
.test > td
 {
  position:relative;
}
.test
{
  position:relative;
}

【问题讨论】:

  • 尝试检查 php 类是否显示预期的输出。否则你可以在你的数据库类执行之后添加一些工作示例或放置最终结果代码。
  • 这是它工作的屏幕截图 - imgur.com/a/ao7ly 我想要发生的是,当我将鼠标悬停在日期/时间上时,它会在其右侧打开另一个框(就像嵌套的 HTML/CSS下拉)目前,什么都没有发生,。
  • 可能是您加载表格的格式不正确。检查一下。试试这个
    Date Time
    今天 10.10
    测试
  • 感谢您的帮助,我已经用我目前所拥有的和正在得到的内容更新了我的问题 - imgur.com/a/U4I3Z 比以前更接近

标签: php html css mysql html-table


【解决方案1】:

你需要将position:relative;应用到.test,否则.drop2content会出现在屏幕外。

.test {
  position:relative;
}

您可能必须改为执行以下 CSS,具体取决于您想要的结果。

.test > td {
  position:relative;
}

【讨论】:

  • 我自己做这个吗?还是在 .test:hover .drop2content 部分?尝试过 >
  • 我已经更新了我的答案。它现在显示您需要添加的 CSS。
  • 感谢您的帮助,我已经用我目前所拥有的和正在得到的内容更新了我的问题 - imgur.com/a/U4I3Z 比以前更接近......
  • 您不必同时添加这两种样式。你只需要一个。单独测试它们,看看它如何影响位置。
【解决方案2】:

如果你从 php 得到的结果可能是正确的。这将是工作。试试这个,而不是在我之前在评论中向您建议的表格中添加 div 更改。

这里是例子

.dropdown
{
   position: relative;
   display: inline-block;
}

/* Dropdown Content (Hidden by Default) */
.dropdown-content
{
   display: none;
   position: absolute;
   z-index: 1;
   margin-top:2%;
   background-color:white;
}

/* Show the dropdown menu on hover */
.dropdown:hover .dropdown-content
{
   display: block;
}

.drop2content
{
  display:none;
  position:absolute;
  background-color:white;
}

.test:hover .drop2content
{
  display:block;
  position:absolute;
  left:100%;
  top:0%;

}
.test > td
 {
  position:relative;
}
.test
{
  position:relative;
}
<div class='dropdown'>
<span class='label label-success'>Domain Found In History</span>
<div class='dropdown-content'>
                   100.0.0.1
</div>
 </div>
              
  <table class='table table-condensed main'>
    <tr>
     <th>Date</th>
      <th>Time</th>
    </tr>
          
     <tr class='test'>
        <td>2017/06/18</td>
        <td>7.55 pm</td>
      
        <td>
        <table class='drop2content'>
              <tr>
                   <td>Test </td>
              </tr> 
         </table>
        <td>
    </tr>
 </table>

【讨论】:

    【解决方案3】:

    主要问题

    table element(即&lt;table&gt;)的permitted content 不包括div 元素。

    允许的内容
    按以下顺序:
    - 一个可选的&lt;caption&gt; 元素
    - 零个或多个 &lt;colgroup&gt; 元素,
    - 一个可选的&lt;thead&gt; 元素
    - 以下任一项:
    - 零个或多个 &lt;tbody&gt; 元素
    - 一个或多个 &lt;tr&gt; 元素
    - 一个可选的 &lt;thead&gt; 元素
    1

    因此&lt;div class='drop2content'&gt;&lt;table class='table table-condensed'&gt; 的直接子级时的无效内容

    选项:

    1. 保持表格结构并将&lt;div class='drop2content'&gt;转换为具有相同类名的表格单元格(即&lt;td&gt;)。然后删除选择器.test:hover .drop2content 上的绝对位置,这样就可以了:

      .test:hover  .drop2content {
           display: block;
      }
      

      this phpfiddle 中查看演示。

    2. 不要使用 HTML 表格,而是使用带有表格布局的 div 元素(有关更多信息,请参阅this post)。

      所以添加这个CSS:

      .table {
          display: table;
      }
      .row {
          display: table-row;
      }
      .table-header, .cell {
          display: table-cell;
          padding: 2px;
      }
      

      然后启动表:

      echo "<div class='table table-condensed'>";
      echo "<div class='row'>";
      echo "<div class='table-header'>Date</div>";
      echo "<div>Time</div>";
      echo "</div>";
      

      对于每一行:

      echo "<div class='row test'>";
      echo "<div class='cell'>".$row["date"]."</div>";
      echo "<div class='cell'>".$row["time"]."</div>";
      echo "<div class='drop2content'>";
      echo "<div>Test </div>";
      echo "</div>";
      echo "</div>";
      

      就像在选项 #1 中一样,删除选择器 .test:hover .drop2content 上的绝对位置,所以它只是:

      .test:hover  .drop2content {
          display: block;
      }
      

      请参阅this phpfiddle 中的演示。

    免责声明:phpfiddle示例中的数据库结果是使用模拟数据库查询的示例类模拟的,因为我可以访问的phpFiddle并没有真正的数据库连接。


    1https://developer.mozilla.org/en-US/docs/Web/HTML/Element/table#table-mdls

    【讨论】:

      猜你喜欢
      • 2016-04-24
      • 2022-01-20
      • 2022-12-17
      • 1970-01-01
      • 1970-01-01
      • 2023-03-22
      • 1970-01-01
      • 2011-10-02
      • 1970-01-01
      相关资源
      最近更新 更多