jQuery 在悬停时显示工具提示

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19935367/
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-27 00:50:37  来源:igfitidea点击:

Show tooltip on hover

javascriptjqueryhtmlcss

提问by Andy Andy

Fiddle:http://jsfiddle.net/G38nx/

小提琴:http: //jsfiddle.net/G38nx/

So I have this table. I want to show a tooltip while rolling over certain td. For example pointing at cell with value 27 should show a tooltip "160cm:70kg".

所以我有这张桌子。我想在滚动某些 td 时显示工具提示。例如,指向值为 27 的单元格应显示工具提示“160cm:70kg”。

Any easy way to do it?

有什么简单的方法可以做到吗?

<table>
    <tr>
        <th></th><th>50kg</th><th>55kg</th><th>60kg</th><th>65kg</th><th>70kg</th>
    </tr>

    <tr>
        <th>160cm</th><td>20</td><td>21</td><td>23</td><td>25</td><td>27</td>
    </tr>

    <tr>
        <th>165cm</th><td>18</td><td>20</td><td>22</td><td>24</td><td>26</td>
    </tr>

    <tr>
        <th>170cm</th><td>17</td><td>19</td><td>21</td><td>23</td><td>25</td>
    </tr>

    <tr>
        <th>175cm</th><td>16</td><td>18</td><td>20</td><td>22</td><td>24</td>
    </tr>
</table>

CSS:

CSS:

table {
    border-spacing: 0;
    border-collapse: collapse;
    overflow: hidden;
    z-index: 1;
}

td, th, .ff-fix {
    cursor: pointer;
    padding: 10px;
    position: relative;
}

td:hover::after,
.ff-fix:hover::after { 
    background-color: #ffa;
    content: '
function firefoxFix() {

    if ( /firefox/.test( window.navigator.userAgent.toLowerCase() ) ) {

        var tds = document.getElementsByTagName( 'td' );

        for( var index = 0; index < tds.length; index++ ) {
            tds[index].innerHTML = '<div class="ff-fix">' + tds[index].innerHTML + '</div>';                     
        };

        var style = '<style>'
            + 'td { padding: 0 !important; }' 
            + 'td:hover::before, td:hover::after { background-color: transparent !important; }'
            + '</style>';
        document.head.insertAdjacentHTML( 'beforeEnd', style );

    };

};

firefoxFix();
a0'; height: 10000px; left: 0; position: absolute; top: -5000px; width: 100%; z-index: -1; } tr:hover{ background-color: #ffa; } }

JS:

JS:

$('td').each(function () {
    var myIndex = $(this).index() + 1;
    var myTitle = $(this).closest('tr').find('th').text();
    myTitle += ":";
    myTitle += $('tr:first-child th:nth-child(' + myIndex + ')').text(); 

    $(this).attr('title', myTitle);

    $(this).tooltip();
});

采纳答案by isherwood

Here's how you might create the titles. It's fairly easy from that point to apply jQueryUI Tooltips, which allow custom styling.

以下是您创建标题的方法。从那时起,应用允许自定义样式的 jQueryUI 工具提示相当容易。

http://jsfiddle.net/isherwood/G38nx/25

http://jsfiddle.net/isherwood/G38nx/25

$(this).tooltip({
    content: "<img src='http://placehold.it/50x50' />",
    show: false,
    hide: false
});

Update: Per your comment, here's an example with image content and instant (no transition) show and hide:

更新:根据您的评论,这是一个包含图像内容和即时(无过渡)显示和隐藏的示例:

http://jsfiddle.net/isherwood/G38nx/31

http://jsfiddle.net/isherwood/G38nx/31

<th data-image-src="<img src='/images/myImage.jpg' />">...</th>

You'd have to use a data attribute to specify images for each cell, something like:

您必须使用数据属性为每个单元格指定图像,例如:

$(this).tooltip({
    content: $(this).attr('data-img-src'),
    show: false,
    hide: false
});

... and then:

... 进而:

<div id="uguu"></div>

回答by kei

DEMO

演示

html

html

$("td").hover(function (e) {
    var idx = $(this).index();
    $("#uguu")
        .css("top", $(this).offset().top)
        .css("left", $(this).offset().left+40)
        .html($(this).siblings("th").text()+":"+$("table tr").first().find("th").eq(idx).text())
        .show();
}, function () {
    $("#uguu").hide();
});

jQuery

jQuery

#uguu {
    position:absolute;
    display:none;
    width:100px;
    height:20px;
    background-color:#cef;
    z-index:400;
    border:1px solid ecf;
}

css

css

##代码##