如何使用jquery将工具提示添加到"td"?

Spo*_*com 8 html jquery

我需要使用jquery为我的表中的"td"元素添加工具提示/ alt.

有人可以帮我吗?

我试过了:

var tTip ="Hello world";
$(this).attr("onmouseover", tip(tTip));
Run Code Online (Sandbox Code Playgroud)

我已经确认我使用"td"作为"这个".

**编辑:**我可以通过使用"alert"命令捕获"td"元素并且它可以工作.所以由于某种原因,"提示"功能不起作用.谁知道为什么会这样?

nic*_*ckf 24

$(this).mouseover(function() {
    tip(tTip);
});
Run Code Online (Sandbox Code Playgroud)

更好的方法可能是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>
Run Code Online (Sandbox Code Playgroud)

然后使用此代码:

$('#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();
}
Run Code Online (Sandbox Code Playgroud)