HTML表格中单元格的工具提示(无Javascript)

时间:2012-12-12 17:11:15

标签: javascript html-table

是否可以为没有JavaScript的表格单元格提供工具提示。不能使用它。

6 个答案:

答案 0 :(得分:151)

你尝试过吗?

<td title="This is Title">

它在Firefox v 18(Aurora),Internet Explorer 8&amp; Google Chrome v 23x

答案 1 :(得分:16)

是。您可以在单元格元素上使用title属性,可用性较差,或者您可以使用CSS工具提示(几个现有问题,可能是这个问题的重复)。

答案 2 :(得分:11)

Mudassar Bashir使用&#34;标题&#34;排名最高的答案属性似乎是最简单的方法,但它可以减少对注释/工具提示显示方式的控制。

我发现Christophe对自定义工具提示类的回答似乎可以更好地控制注释/工具提示的行为。由于提供的演示不包含表格,根据问题,这里是a demo that includes a table

请注意&#34;位置&#34; span的父元素的样式(在本例中为a),必须设置为&#34; relative&#34;这样评论在显示时不会推动表格内容。我花了一点时间才弄明白。

&#13;
&#13;
#MyTable{
  border-style:solid;
  border-color:black;
  border-width:2px
}

#MyTable td{
  border-style:solid;
  border-color:black;
  border-width:1px;
  padding:3px;
}

.CellWithComment{
  position:relative;
}

.CellComment{
  display:none;
  position:absolute; 
  z-index:100;
  border:1px;
  background-color:white;
  border-style:solid;
  border-width:1px;
  border-color:red;
  padding:3px;
  color:red; 
  top:20px; 
  left:20px;
}

.CellWithComment:hover span.CellComment{
  display:block;
}
&#13;
<table id="MyTable">
  <caption>Cell 1,2 Has a Comment</caption>
  <thead>
    <tr>
      <td>Heading 1</td>
      <td>Heading 2</td>
      <td>Heading 3</td>
    </tr>
  </thead>
  <tbody>
    <tr></tr>
      <td>Cell 1,1</td>
      <td class="CellWithComment">Cell 1,2
        <span class="CellComment">Here is a comment</span>
      </td>
      <td>Cell 1,3</td>
    <tr>
      <td>Cell 2,1</td>
      <td>Cell 2,2</td>
      <td>Cell 2,3</td>
    </tr>
  </tbody>
</table>
&#13;
&#13;
&#13;

答案 3 :(得分:5)

您可以使用css和:hover伪属性。这是一个simple demo。它使用以下css:

a span.tooltip {display:none;}
a:hover span.tooltip {position:absolute;top:30px;left:20px;display:inline;border:2px solid green;}

请注意,旧浏览器对以下内容的支持有限:hover。

答案 4 :(得分:2)

BioData41添加的内容的演变......

将以下内容放在CSS样式

     <style>

        .CellWithComment{position:relative;}

        .CellComment
        {
            visibility: hidden;
            width: auto;
            position:absolute; 
            z-index:100;
            text-align: Left;
            opacity: 0.4;
            transition: opacity 2s;
            border-radius: 6px;
            background-color: #555;
            padding:3px;
            top:-30px; 
            left:0px;
        }   
        .CellWithComment:hover span.CellComment {visibility: visible;opacity: 1;}
</style>

然后,像这样使用它:

        <table>
            <tr>
                <th class="CellWithComment">Category<span class="CellComment">"Ciaooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo"</span></th>
                <th class="CellWithComment">Code<span class="CellComment">"Ciaooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo"</span></th>
                <th>Opened</th>
                <th>Event</th>
                <th>Severity</th>           
                <th>Id</th>
                <th>Component Name</th>
            </tr>
            <tr>
                <td>Table cell</td>
                <td>Table cell</td>
                <td>Table cell</td>
                <td>Table cell</td>
                <td>Table cell</td>
                <td>Table cell</td>
                <td>Table cell</td>
            </tr>
            <tr>
                <td>Table cell</td>
                <td>Table cell</td>
                <td>Table cell</td>
                <td>Table cell</td>
                <td>Table cell</td>
                <td>Table cell</td>
                <td>Table cell</td>
            </tr>
        </table>

答案 5 :(得分:0)

if (data[j] =='B'){
    row.cells[j].title="Basic";
}

在Java脚本中,通过比较Data的值有条件地添加标题。 该表由Java脚本动态生成。