2008-10-03 75 views
7

我需要添加一个工具提示/ ALT到我的表内的“td”元素与jquery。如何使用jquery将工具提示添加到“td”?

有人可以帮我吗?

我想:

var tTip ="Hello world"; 
$(this).attr("onmouseover", tip(tTip)); 

,我已经验证了我现在用的是 “TD” “本”。

**编辑:**我可以通过使用“alert”命令捕获“td”元素,它工作。所以出于某种原因,“提示”功能不起作用。任何人都知道为什么会这样?

回答

24
$(this).mouseover(function() { 
    tip(tTip); 
}); 

更好的方法可能是将title属性放入HTML中。这样,如果有人关闭了JavaScript,他们仍然会得到一个工具提示(尽管不像使用jQuery那样漂亮/灵活)。

<table id="myTable"> 
    <tbody> 
     <tr> 
      <td title="Tip 1">Cell 1</td> 
      <td title="Tip 2">Cell 2</td> 
     </tr> 
    </tbody> 
</table> 

,然后使用此代码:

$('#myTable td[title]') 
    .hover(function() { 
     showTooltip($(this)); 
    }, function() { 
     hideTooltip(); 
    }) 
; 

function showTooltip($el) { 
    // insert code here to position your tooltip element (which i'll call $tip) 
    $tip.html($el.attr('title')); 
} 
function hideTooltip() { 
    $tip.hide(); 
} 
+0

我想这个工作,但由于某种原因,它不... – 2008-10-03 04:08:29

+0

你能详细说明吗?会发生什么?有没有错误信息?你真的有一个叫做“提示”的功能吗? – nickf 2008-10-03 04:10:43

1
var tTip ="Hello world"; 
$(this).mouseover(function() { tip(tTip); }); 
+0

我想这个工作,但由于某种原因,它不... – 2008-10-03 04:06:55

-1

如果你确实想要把这些提示对您的表格单元格而不是你的表格标题 - 他们会更有意义的地方 - 请考虑将它们放在表格单元格中的内容上,它更有意义。

1

grdList - 表ID

TD:第n个孩子(5) - 列

$('#grdList tr td:nth-child(5)').each(function(i) { 
    if (i > 0) { //skip header 
     var sContent = $(this).text(); 
     $(this).attr("title", $(this).html()); 
     if (sContent.length > 20) { 
      $(this).text(sContent.substring(0,20) + '...'); 
     } 
    } 
}); 
3
$('#grdList tr td:nth-child(5)').each(function(i) { 
    if (i > 0) { //skip header 
     var sContent = $(this).text(); 
     $(this).attr("title", $(this).html()); 
     if (sContent.length > 20) { 
      $(this).text(sContent.substring(0,20) + '...'); 
     } 
    } 
}); 

grdList - 表ID

TD:第n个孩子(5) - 列5

相关问题