繁体   English   中英

在工具提示中的鼠标上更改背景颜色

[英]change background color on mouse over in tooltip

当我将鼠标悬停在表格的行上时,将显示图像,而当将鼠标悬停在图像上时,将显示工具提示。 当我将鼠标悬停在工具提示中的选项(即test1,test2)上时,鼠标光标所在的选项应突出显示或更改其背景颜色,以使用户知道他们将要单击工具提示中的哪个选项。 请找到小提琴http://jsfiddle.net/0w9yo8x6/65/

以下是示例javascript代码:

$(function() { 
    var rowData;
    $(document).tooltip({
        items: "img, [data-title]",
        content: function () {
            var element = $(this);
            if (element.is("img"))
             {
                 rowdata = element.attr("data-title");
                 $(document).off('click', '#test');
                 $(document).on('click', '#test', function() {
                     test();
                 });


             }

            return $(this).prop('title');
        },
        show: null, 
        close: function (event, ui) {
            ui.tooltip.hover(

            function () {
                $(this).stop(true).fadeTo(1000, 1);
            },

            function () {
                $(this).stop(true).fadeOut(200, function () {
                    $(this).remove();
                })
            });
        },
        position: {
            my: "left",
            at: "right"
        }
    });
});



$(function () {
  $('.one').attr('title', $('.myTooltipTable').remove().html());
  $(document).tooltip();
});

我认为这就是您要寻找的: http : //jsfiddle.net/0w9yo8x6/66/

您必须注意何时使用ID和何时使用类。

.toolTipHover:hover {
    background-color:lightgray;
}

<table class="myTooltipTable" style="position:absolute;">
    <tr><td> <span id="test" class="toolTipHover">test1</span></td></tr>
     <tr><td> <span id="test" class="toolTipHover">test2</span></td></tr>
 </table>

在上面的html中,您将看到id =“ test”有两种不同的方式吗? 它们中只有一个应该注册(第一个),因为在DOM上,ID必须是唯一的。 所以我实现了一个类toolTipHover并将CSS应用于该类。

我没有触及任何其他代码,但是我建议您回顾一下您的代码,以确保ID是唯一的,并且如果您希望将相同的函数应用于多个元素,则最好添加一个类。

您无需编辑HTML并添加其他类。 您可以通过添加简单的CSS悬停效果来实现此目的:

您只需要添加以下代码:

.ui-tooltip-content span:hover {
    background-color:Orange;
    transition:all 0.4s ease-in-out;
}

请参阅下面的完整代码:

 $(function() { var rowData; $(document).tooltip({ items: "img, [data-title]", content: function () { var element = $(this); if (element.is("img")) { rowdata = element.attr("data-title"); $(document).off('click', '#test'); $(document).on('click', '#test', function() { test(); }); } return $(this).prop('title'); }, show: null, close: function (event, ui) { ui.tooltip.hover( function () { $(this).stop(true).fadeTo(1000, 1); }, function () { $(this).stop(true).fadeOut(200, function () { $(this).remove(); }) }); }, position: { my: "left", at: "right" } }); }); $(function () { $('.one').attr('title', $('.myTooltipTable').remove().html()); $(document).tooltip(); }); 
 td.myData > img { display: none; float:right; height: 19px; } td.myData:hover > img { display: inline-block; } .ui-tooltip-content span { transition:all 0.4s ease-in-out; } .ui-tooltip-content span:hover { background-color:Orange; transition:all 0.4s ease-in-out; } 
 <script> function test(){ alert("test invoked"); } </script> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <link rel="stylesheet" href="//ajax.googleapis.com/ajax/libs/jqueryui/1.11.1/themes/smoothness/jquery-ui.css" /> <script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.11.1/jquery-ui.min.js"></script> <table class="myTooltipTable" style="position:absolute;"> <tr><td> <span id="test" >test1</span></td></tr> <tr><td> <span id="test" >test2</span></td></tr> </table> <br/><br><br> <table border="1"> <tr> <td class="myData">Data1 <img class="one" id="one" data-title="Data1" src="http://ssl.gstatic.com/gb/images/b_5dae6e31.png" width="15" height="15" alt="" /> <img class="two" id="two" src="https://www.webkit.org/blog-files/acid3-100.png" width="15" height="15" style="visibility:hidden;"/> </td> </tr> <tr> <td class="myData">Data2 <img class="one" id="one" data-title="Data2" src="http://ssl.gstatic.com/gb/images/b_5dae6e31.png" width="15" height="15" alt="" /> <img class="two" id="two" src="https://www.webkit.org/blog-files/acid3-100.png" width="15" height="15" style="visibility:hidden;"/> </td> </tr> <tr> <td class="myData">Data3 <img class="one" id="one" data-title="Data3" src="http://ssl.gstatic.com/gb/images/b_5dae6e31.png" width="15" height="15" alt="" /> <img class="two" id="two" src="https://www.webkit.org/blog-files/acid3-100.png" width="15" height="15" style="visibility:hidden;"/> </td> </tr> </table> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM