Html 表格中超级简单的 CSS 工具提示,为什么不显示,我可以让它工作吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2736021/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 02:53:30  来源:igfitidea点击:

Super simple CSS tooltip in a table, why is it not displaying and can I make it work?

htmlcsshtml-tabletooltip

提问by Kyle

I have been trying to implement many different tooltips on this page for my client, he's adamant that we have a picture of the product show up when you hover over the product name in the order page. I decided to use the super simple CSS tooltip, it's very easy to implement and does exactly what we want. It works on a dynamic page, the others I tried didn't.

我一直在尝试为我的客户在此页面上实施许多不同的工具提示,他坚持认为当您将鼠标悬停在订单页面中的产品名称上时,我们会显示产品图片。我决定使用超级简单的 CSS 工具提示,它很容易实现并且完全符合我们的要求。它适用于动态页面,我尝试过的其他页面没有。

I have made an example here: CSS tooltip in table example.<-- updated to remove errors.

我在这里做了一个例子:表格示例中的 CSS 工具提示.<--更新以删除错误

HTML:

HTML:

<table class="mytable" id="cart">

  <tr id="titles">
    <th id="varekodetext">Varekode</th>
    <th id="produkttext">Produkt</th>
    <th id="pristext">Pris</th>
    <th id="emptee">&nbsp;</th>
    <th id="antalltext">Antall</th>
    <th id="pristotaltext">Pris Total</th>
    <th id="sletttext">Slett</th>
  </tr>
  <tbody>
    <tr class="even first" id="topborder" height="40px">
      <td class="cart2Varekode"> <span>39261-02 </span>
      </td>
      <td class="cart2Produkt"> <a href="/Plantronics-CS361N.11" target="_blank" class="tooltip" title="Plantronics CS361N"> 
                Plantronics CS361N
                <span> 
                    <img src="http://www.euroworker.no/public/upload/productimage/220-353-2.jpg" alt="Plantronics CS361N" /> 

                </span> 
                </a>
        <p>
        </p>
      </td>
      <td class="cart2Price">
        <span class="actualPrice">2390.-</span>
      </td>
      <td class="cart2Salg">
        <span class="orderlistPrice"></span>
      </td>
      <td class="antallbox">
        <span class="cartQuant"><input name="item_1363" class="DG-spinner" id="text" type="text" value="1"/> <span class="cartUpdate"><button type="submit" class="submit" id="rfrsh_btn" name="saveFields" title="Oppdater" value="">&nbsp;</button></span>
        </span>
      </td>
      <td class="cart2Total">
        <span class="basePrice">2390.-</span><span class="actualPrice">2390.-</span>
      </td>
      <td class="delete">
        <a class="slett" href="../..//order/delete/1363?return=" title="Slett">Remove</a>
      </td>
    </tr>
    <tr class="odd last" id="topborder" height="40px">
      <td class="cart2Varekode"> <span>7050-20</span>
      </td>
      <td class="cart2Produkt"> <a href="/Target-7050CC-Duo-UNC.7" target="_blank" class="tooltip" title="Target 7050CC Duo UNC">Target 7050CC Duo UNC<span>
      <img src="upload/productimage/7-250-2.jpg?1251022192" alt="Target 7050CC Duo UNC" />               
                </span> 
                </a>

        <p>
          <div class="productOptions" style="color:#b2b2b2;">

            <div class="nonEditableOption">
              Skal tilkobles: Alcatel IP Touch
            </div>

            <div class="productOptionsMenu">
              <a style="color:#6c8aa2;" href="/order/options/1377" ajax="/order/optionForm/1377">Endre valg</a>
            </div>
          </div>

        </p>

      </td>
      <td class="cart2Price"><span class="actualPrice">899.-</span>
      </td>
      <td class="cart2Salg">
        <span class="salg" title="Rabatt">&nbsp;</span>

        <span class="orderlistPrice" title="Opprinnelige prisen"> 
                    (1599.-)
                </span>
      </td>
      <td class="antallbox">
        <span class="cartQuant"><input name="item_1377" class="DG-spinner" id="text" type="text" value="1"/> 

                    <span class="cartUpdate"><button type="submit" class="submit" id="rfrsh_btn" name="saveFields" title="Oppdater" value="">&nbsp;</button></span>
        </span>
      </td>


      <td class="cart2Total">

        <span class="basePrice">899.-</span><span class="actualPrice">899.-</span>

      </td>
      <td class="delete">

        <a class="slett" href="../..//order/delete/1377?return=" title="Slett">Remove</a>

      </td>
    </tr>
    <tr>
      <td colspan="6" class="cols-six"></td>
    </tr>
    <tr id="topborder-tr">
      <td class="subTotalCaption2">&nbsp;</td>
      <td colspan="4" class="subTotalCaption2">Mva (25%):</td>
      <td class="amount taxAmount2">822.-</td>
      <td>&nbsp;</td>



    </tr>
    <tr>
      <td class="subTotalCaption2">&nbsp;</td>
      <td colspan="4" class="subTotalCaption2">Totalt:</td>
      <td class="subTotal2">4111.-</td>
      <td>&nbsp;</td>

    </tr>
    <tr>
      <td colspan="5"></td>
      <td class="cartQuant"></td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td colspan="7">
        <a href="/" class="continueShopping"><span>&nbsp;</span></a>
        <div class="checkoutButtons">
          <a href="/checkout" class="proceedToCheckout" onclick="return Order.submitCartForm(this);"><span>&nbsp;</span></a>

        </div>
      </td>
    </tr>
    <script type="text/javascript">
      new Order.OptionLoader($('cart'));

    </script>

  </tbody>

</table>

CSS:

CSS:

.cart2Produkt a:hover {
  background: #ffffff;
  text-decoration: none;
  z-index: 999;
}

/*BG color is a must for IE6*/

.tooltip {
  text-align: left;
}

.cart2Produkt a.tooltip span {
  z-index: 999;
  display: block;
  position: absolute;
  left: -999px;
  opacity: 0;
  padding: 2px 3px;
  margin-left: 8px;
  width: 130px;
  -webkit-transition: opacity;
  -webkit-transition-timing-function: ease-in;
  -webkit-transition-duration: 500ms;
}

.cart2Produkt a.tooltip:hover span {
  z-index: 999;
  display: block;
  position: absolute;
  right: 50%;
  opacity: 1;
  background: #ffffff;
  border: 1px solid #cccccc;
  color: #6c6c6c;
  top: -35px;
  left: -15px;
  z-index: 10;
  border-radius: 5px;
  -moz-border-radius: 5px;
  -webkit-border-radius: 5px;
  text-align: center;
  vertical-align: middle;
  padding: 1px;
  -webkit-transition: opacity;
  -webkit-transition-timing-function: ease-out;
  -webkit-transition-duration: 500ms;
}


/**************IGNORE BELOW*************/

#topborder {
  /*border-top: 1px #ccc solid;*/
  padding-bottom: 10px;
}

#topborder-tr {
  border-top: 1px #ccc solid;
}

.mytable {
  width: 100%;
  margin-bottom: 20px;
}

.mytable,
.mytable th,
.mytable tr,
.mytable td {
  border: 0;
  letter-spacing: 1px;
}

.mytableborders,
.mytableborders th,
.mytableborders tr,
.mytableborders td {
  border: 1px #ccc solid;
  /*width:670px;*/
}

.mytable td,
.mytable td span {
  padding-bottom: 0;
  padding-top: 5px;
  /*border-top: 1px #ccc solid;*/
  vertical-align: middle;
  font-size: 12px;
}

.mytable th {
  vertical-align: bottom;
  height: 30px;
  padding-bottom: 5px;
}

#titles {
  margin: 0px auto 0px auto;
  width: 100%;
  padding-bottom: 50px;
  z-index: 1;
  border: 1px #ccc solid;
  border-left: none;
  border-right: none;
}

.cart2Produkt a {
  color: #0a5692;
  text-decoration: none;
  line-height: 15px;
  clear: both;
  z-index: 999;
}

productOptionsMenu {
  clear: both;
}

#pristotaltext p,
#varekodetext p,
#sletttext p,
#pristext p,
#antalltext p,
#produkttext p {
  font: 12px/11px "Helvetica";
  margin: 0px;
  color: #828273;
  text-align: left;
}

#thinline,
#thinlinecopy {
  background: url(../../upload/thinline.png) no-repeat;
  visibility: visible;
  position: absolute;
  left: 0px;
  z-index: 4;
  width: 747px;
  height: 1px;
}

#thinlinefakt {
  background: url(../../upload/thinline.png) no-repeat;
  visibility: visible;
  position: absolute;
  top: 100px;
  left: 0px;
  z-index: 4;
  width: 747px;
  height: 1px;
}

#thinlinefakt2 {
  background: url(../../upload/thinline.png) no-repeat;
  background-position: bottom;
  visibility: visible;
  position: absolute;
  top: 205px;
  left: 0px;
  z-index: 4;
  width: 747px;
  height: 28px;
  margin: 5px;
  margin-left: 10px;
  margin-top: 0px;
  padding: 5px;
  color: #000000;
  font-size: 22px;
  font-family: "Helvetica";
}

.Fadresse {
  display: block;
  margin: 5px;
  padding: 0;
  color: #000000;
  font-size: 22px;
  font-family: "Helvetica";
}

#thinlinecopylever {
  background: url(../../upload/thinline.png) no-repeat;
  background-repeat: repeat-x;
  position: relative;
  left: 0px;
  top: -3px;
  width: 100%;
  height: 1px;
  padding: 0px;
}

#varekodetext {
  width: 65px;
  padding-left: 5px;
  text-align: center;
}

#produkttext {
  width: 150px;
}


/*#sgproductview  {

margin: 0px auto 0px auto;
width: 595px;
height:1%;
z-index:1;
}*/

#thinlinecopy {
  top: 1px;
}

#antalltext {
  width: 25px;
  text-align: center;
}

#pristotaltext {
  width: 10%;
  text-align: right;
}

#sletttext {
  width: 10%;
  text-align: center;
}

#thinline {
  top: 19px;
  position: absolute;
}

#pristext {
  width: 24px;
  text-align: center;
}

#emptee {
  background-color: none;
  width: 40px;
}


/*****************************************CONTENT*/

.cart2Produkt p,
.cart2Varekode p,
.cart2Salg p {
  font: 13px/11px "Helvetica";
  margin: 0px;
  letter-spacing: 1px;
  text-align: left;
}

.cart2Produkt p a {
  color: #000000;
}

.cart2Total p {
  font: 13px/11px "Helvetica";
  margin: 0px;
  letter-spacing: 1px;
  text-align: left;
  color: #000000;
}

.cart2Varekode {
  visibility: visible;
  z-index: 2;
  text-align: center;
  vertical-align: top;
  padding-top: 3px;
  font-weight: bold;
}

.cart2Produkt {
  visibility: visible;
  text-decoration: none;
  text-align: left;
  position: relative;
  width: 28%;
  font-weight: bold;
  padding-bottom: 2px;
}

.cart2Produkt a {
  white-space: nowrap;
}

.cart2Price {
  visibility: visible;
  z-index: 2;
  text-align: right;
  width: 6%;
  position: relative;
  font-weight: bold;
}

.cart2Price p {
  font: 13px/11px "Helvetica";
  margin: 0px;
  letter-spacing: 1px;
  text-align: right;
  width: 80%;
}

.cart2Total p {
  font-weight: bold;
  text-align: right;
}

.cart2Salg p {
  color: #B2B2B2;
  width: 20%;
}

.cart2Total {
  visibility: visible;
  z-index: 2;
  text-align: right;
  font-weight: bold;
}

.antallbox {
  white-space: nowrap;
  text-align: center;
}

.antallbox input {
  text-align: right;
  outline: none;
  width: 30px;
}

.antallbox input:focus {
  text-align: right;
  outline: none;
  border: 1px #000 solid;
  background-color: #F0F7FD;
}

.cartQuant {
  width: 30px;
  /*    white-space: nowrap;*/
  text-align: left;
  margin-top: 5px;
}

.cart2Salg {
  visibility: visible;
  z-index: 2;
  padding: 0;
  margin: 0;
}

.orderlistPrice {
  text-decoration: line-through;
  font-weight: bold;
  color: #b2b2b2;
  font-size: 12px;
}

.salg {
  background: url(../../upload/salg.png) no-repeat;
  visibility: visible;
  z-index: 2;
  width: 28px;
  height: 24px;
  display: inline-block;
  position: relative;
}

.delete {
  visibility: visible;
  z-index: 2;
  height: 21px;
  background: none;
  text-align: center;
}

.slett {
  width: 24px;
  height: 19px;
  background-image: url(../../upload/delete_box_sprite.png);
  background-position: 0 0;
  text-decoration: none;
  display: inline-block;
}

.slett:hover {
  width: 24px;
  height: 19px;
  background-image: url(../../upload/delete_box_sprite.png);
  background-position: -24px 0;
  text-decoration: none;
  display: inline-block;
}

.productOptions {
  background-color: #fff;
  postion: absolute;
  visibility: visible;
  display: block;
  top: 3px;
  padding-left: 5px;
  padding-bottom: 10px;
}

.nonEditableOption {
  background-color: #fff;
  float: left;
  postion: absolute;
  margin-right: 10px;
  visibility: visible;
  display: block;
}

This page used to be displayed using divs, I have since changed it to a table, as it's tabular data and easier to work with. It worked fine when it used divs, now it's in a table, it won't display the span on hover.

此页面过去使用 div 显示,此后我将其更改为表格,因为它是表格数据且更易于使用。它在使用 div 时运行良好,现在它在一个表格中,它不会在悬停时显示跨度。

My questions are:

我的问题是:

  1. Why is it not working?

  2. How can I make it work?

  3. If not, does anyone know another super easy to implement tooltip that can work properly on a dynamic page?
  1. 为什么它不起作用?

  2. 我怎样才能让它工作?

  3. 如果没有,有没有人知道另一个可以在动态页面上正常工作的超级容易实现的工具提示?

Here's the DIV tooltip for reference: DIV display tooltip.

以下是供参考的 DIV 工具提示:DIV display tooltip

HTML:

HTML:

<div id="JSwrap">
    <div id="cart2Produkt">
        <p><a href="/Target-7050-Softphone-USB-Duo.220" target="_blank" class="tooltip" title="Target 7050 Softphone USB Duo ">Target 7050 Softphone USB Duo
    <span><img src="http://www.euroworker.no/public/upload/productimage/220-353-2.jpg?1251413379" alt="Target 7050 Softphone USB Duo " />
<br /> 
  </span> 
</a>
        </p>
    </div>
</div>

CSS:

CSS:

#JSwrap {
  /*for jsfiddle only*/
  position: absolute;
  left: 100px;
  top: 50px;
}

#cart2Produkt {
  border: 1px solid #ccc;
  width: 500px;
  text-align: left;
  padding: 10px;
}

#cart2Produkt a.tooltip span {
  z-index: 999;
  display: block;
  position: absolute;
  left: -999px;
  opacity: 0;
  padding: 2px 3px;
  margin-left: 8px;
  width: 130px;
  -webkit-transition: opacity;
  -webkit-transition-timing-function: ease-in;
  -webkit-transition-duration: 500ms;
}

#cart2Produkt a.tooltip:hover span {
  z-index: 999;
  display: block;
  position: absolute;
  right: 50%;
  opacity: 1;
  background: #ffffff;
  border: 1px solid #cccccc;
  color: #6c6c6c;
  top: -35px;
  left: -15px;
  z-index: 10;
  border-radius: 5px;
  -moz-border-radius: 5px;
  -webkit-border-radius: 5px;
  text-align: center;
  vertical-align: middle;
  padding: 1px;
  -webkit-transition: opacity;
  -webkit-transition-timing-function: ease-out;
  -webkit-transition-duration: 500ms;
}

#cart2Produkt img {
  z-index: 999;
}

Edit: Just noticed it half works in IE8.

编辑:刚刚注意到它在 IE8 中工作了一半。

Update:It's now working in all browsers, thanks to Daniel pointing out my CSS errors, but is there a way to make it display outside of the table cell?

更新:由于 Daniel 指出了我的 CSS 错误,它现在可以在所有浏览器中使用,但是有没有办法让它显示在表格单元格之外?

回答by stefanglase

Find a more readable working solution here:

在此处找到更具可读性的工作解决方案:

.cart { width: 100%; }

.hasTooltip span {
    display: none;
    color: #000;
    text-decoration: none;
    padding: 3px;
}

.hasTooltip:hover span {
    display: block;
    position: absolute;
    background-color: #FFF;
    border: 1px solid #CCC;
    margin: 2px 10px;
}
<table class="cart"> 
    <tr> 
        <th id="pos">Pos</th> 
        <th id="name">Product</th> 
        <th id="price">Price</th> 
    </tr> 
    <tbody> 
        <tr>
            <td>1</td>
            <td>
                <a href="productdetails.htm" class="hasTooltip">Flatscreen 
                    <span>New visual experience!</span>
                </a>
            </td>
            <td>19.99</td>
        </tr>
        <tr>
            <td>2</td>
            <td>
                <a href="productdetails.htm" class="hasTooltip">Headset 
                    <span>Inject music directly into your ears!</span>
                </a>
            </td>
            <td>19.99</td>
        </tr>
    </tbody> 
</table>

回答by Daniel Vassallo

You seem to have a problem with CSS. Simply using styles of the working JSFiddle demo should fix it. You had the span set as display:none, however it looks like this tooltip works by hiding the image from the screen, using left:-999px.

您似乎对 CSS 有问题。只需使用工作 JSFiddle 演示的样式就可以解决它。您已将跨度设置为display:none,但看起来此工具提示通过使用left:-999px.

Check this: http://jsfiddle.net/WNzhJ/

检查这个:http: //jsfiddle.net/WNzhJ/

回答by meo

I have checked your code, and as you can see by the jsfiddle sytax hightlighting there are a lot of errors in your code: for example the span you who starts in your a finishes outside your a. The CSS should work like this. There are a few more errors. i think if you correct them its gonna work. (simply pass the w3c check, then anything should be just fine)

我已经检查了您的代码,正如您通过 jsfiddle 语法突出显示所看到的,您的代码中有很多错误:例如,从 a 开始的跨度在 a 之外完成。CSS 应该像这样工作。还有几个错误。我想如果你纠正它们就会起作用。(只需通过 w3c 检查,然后任何事情都应该没问题)

edit: i missed a thing your link should be position: relative; because your tooltip is position: absolute, the absolute always is relative to the last relative parent.

编辑:我错过了一个你的链接应该是位置的东西:相对;因为您的工具提示是位置:绝对,绝对总是相对于最后一个相对父级。

also remove the opacity: 0 in your original span

还删除不透明度:原始跨度中的 0

回答by Alan Stewart

I was trying the same thing, trying to get a CSS tooltip to appear outside of the table cell. In my case the table cell was only 1px wide so it HAD to show up outside. I found out that by making the :hover spanposition: absolute;and then adding margin: -37px 0px 0px -50px;I could use that to move the tooltip around. BUT! If I tried to adjust the top/left/bottom/right then the absolute setting positioned it absolutely on the screen, not relative to the cell.

我正在尝试同样的事情,试图让 CSS 工具提示出现在表格单元格之外。在我的情况下,表格单元格只有 1px 宽,所以它必须显示在外面。我发现通过制作:hover spanposition: absolute;然后添加margin: -37px 0px 0px -50px;我可以使用它来移动工具提示。但!如果我尝试调整顶部/左侧/底部/右侧,则绝对设置将其绝对定位在屏幕上,而不是相对于单元格。

Hope this helps someone.

希望这可以帮助某人。