【问题标题】:change background color on mouse over in tooltip在工具提示中更改鼠标悬停的背景颜色
【发布时间】:2015-01-14 20:02:26
【问题描述】:

当我将鼠标悬停在表格中的行上时,会显示图像,当鼠标悬停在图像上时会显示工具提示。 当我将鼠标悬停在工具提示中的选项上时(即 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();
});

【问题讨论】:

    标签: javascript jquery html css


    【解决方案1】:

    我想这就是你要找的东西: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 是唯一的,并且如果您希望应用相同功能的多个元素,也许添加一个类将是最好的选择。

    【讨论】:

      【解决方案2】:

      您无需编辑 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>

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-03-07
        • 2010-12-25
        • 1970-01-01
        • 2018-10-16
        • 1970-01-01
        • 2013-10-07
        • 2015-09-29
        相关资源
        最近更新 更多