【问题标题】:Showing Text Box On Hover (In Table)在悬停时显示文本框(在表格中)
【发布时间】:2019-03-04 20:46:07
【问题描述】:

我一直在尝试在表格中创建悬停文本。 我一直在尝试添加它以将其悬停在此 <td class="text-left" id="caster_num">1</td> 上,但到目前为止我还没有运气。我一直在关注这个https://www.w3schools.com/css/css_tooltip.asphttps://www.w3schools.com/howto/howto_css_tooltip.asp。有人可以帮助我吗?

*我希望文本显示在被悬停的文本上(在本例中为“1”)。

谢谢。

body {
  background-color: #2c313a;
  font-family: "Roboto", helvetica, arial, sans-serif;
  font-size: 16px;
  font-weight: 400;
}

div.table-title {
  display: block;
  margin: auto;
  max-width: 600px;
  padding: 5px;
  width: 100%;
}

.table-title h3 {
  color: #fafafa;
  font-size: 30px;
  font-weight: 400;
  font-style: normal;
  font-family: "Roboto", helvetica, arial, sans-serif;
  text-shadow: -1px -1px 1px rgba(0, 0, 0, 0.1);
  text-transform: uppercase;
}


/*** Table Styles **/

.table-fill {
  background: white;
  border-collapse: collapse;
  height: 320px;
  margin: auto;
  max-width: 600px;
  padding: 5px;
  width: 100%;
  box-shadow: 0 5px 10px rgba(0, 0, 0, 0.1);
  animation: float 5s infinite;
}

th {
  color: #D5DDE5;
  background: #1b1e24;
  border-bottom: 4px solid #9ea7af;
  border-right: 1px solid #343a45;
  font-size: 23px;
  font-weight: 100;
  padding: 10px;
  text-align: left;
  text-shadow: 0 1px 1px rgba(0, 0, 0, 0.1);
  vertical-align: middle;
}

th:last-child {
  border-right: none;
}

tr {
  border-top: 1px solid #C1C3D1;
  border-bottom: 1px solid #C1C3D1;
  color: #666B85;
  font-size: 16px;
  font-weight: normal;
  text-shadow: 0 1px 1px rgba(256, 256, 256, 0.1);
}

tr:hover td {
  background: #4E5066;
  color: #FFFFFF;
  border-top: 1px solid #22262e;
}

tr:first-child {
  border-top: none;
}

tr:last-child {
  border-bottom: none;
}

tr:nth-child(odd) td {
  background: #EBEBEB;
}

tr:nth-child(odd):hover td {
  background: #4E5066;
}

td {
  background: #FFFFFF;
  padding: 10px;
  text-align: left;
  vertical-align: middle;
  font-weight: 300;
  font-size: 18px;
  text-shadow: -1px -1px 1px rgba(0, 0, 0, 0.1);
  border-right: 1px solid #C1C3D1;
}

td:last-child {
  border-right: 0px;
}

th.text-left {
  text-align: left;
}

th.text-center {
  text-align: center;
}

th.text-right {
  text-align: right;
}

td.text-left {
  text-align: left;
}

td.text-center {
  text-align: center;
}

td.text-right {
  text-align: right;
}
<body>
  <div id="wrapper">

    <table class="table-fill">
      <thead>
        <tr>
          <th class="text-left">Name</th>
          <th class="text-left">Attribute</th>
        </tr>
      </thead>
      <tbody class="table-hover">
        <tr id="caster">
          <td class="text-left">Caster</td>
          <td class="text-left" id="caster_num">1</td>
        </tr>
      </tbody>
    </table>
  </div>
</body>

【问题讨论】:

  • 我在你的问题中看不到任何 CSS。你想在哪里添加你的文本,你想要纯 css,尤其是:你试过什么?我们将帮助您找出您犯了哪些错误,但我们不会为您编写代码。你的两个链接也是一样的;)
  • 好吧,在你编辑之后:我没有看到任何工具提示类的痕迹,也没有迹象表明你确实尝试过你链接我们的教程,如果你知道你在做什么,这很简单,只需添加跨度在你的 td 中,添加类 tooltip 和 tooltiptext,以及 awample css,它就像一个魅力
  • @Neil 嘿,抱歉,没有将它包含在我发布的 CSS 中,因为我已经尝试了许多不同的编辑,不知道要上传哪个版本...我尝试添加在我的 TD 中跨度并添加类,但使用 DIV 成为巫毒效果(表中的 div)
  • 这就是要走的路,除了你用td替换每个div。不要害怕向我们展示不起作用的代码,至少我们可以看到您有问题的地方:) 如果您的 td 不喜欢 position:relative,您可以将 span 放在工具提示中div,在你的 td 里面 ;)

标签: html css html-table css-tables onhover


【解决方案1】:

您可以悬停该行,然后应用一些 css

.table-fill tr #caster_num { visibility: hidden; color: transparent; }
.table-fill tr:hover #caster_num { visibility: visible; color: black; }

【讨论】:

  • 他想要一个工具提示,而不是隐藏数字,他的教程已经解释了所有需要的内容
【解决方案2】:

这是一个纯 CSS 的解决方案,但我不得不稍微修改您的 html。不确定您是否希望工具提示覆盖触发器或位于触发器上方,但此示例只是将工具提示直接放在触发器的右侧。您可以根据自己的需要进行修改。

如果您不想包含额外的 span,则需要一些额外的 javascript 来获取之前的 td 的内容。

这都是基于https://www.w3schools.com/howto/howto_css_tooltip.asp

https://codepen.io/carbonspace/pen/rqaVzb

HTML

<table class="table-fill">
  <thead>
    <tr>
      <th class="text-left">Name</th>
      <th class="text-left">Attribute</th>
    </tr>
  </thead>
  <tbody class="table-hover">
    <tr id="caster">
      <td class="text-left">Caster</td>
      <td class="text-left tooltipTrigger" id="caster_num">1
        <span class="tooltip">Caster</span>
      </td>
    </tr>
  </tbody>
</table>

CSS

.tooltipTrigger .tooltip {
  display: none;
}
.tooltipTrigger:hover {
  cursor: pointer;
}
.tooltipTrigger:hover .tooltip {
  display: inline;
  position: relative; /* relative to .tooltipTrigger */
  left: 10px;
  border: 1px solid #ccc;
}

【讨论】:

    猜你喜欢
    • 2015-01-23
    • 2014-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多