简体   繁体   English

如何使用jQuery动态地将HTML叠加层添加到表行?

[英]How can I dynamically add an HTML overlay to a table row using jQuery?

I have the table structure given below: 我有下面给出的表结构:

<table>
      <tr id="Row1">
         <td class="Column1">Column</td>
         <td class="Column2">Column2</td>
     </tr>
      <tr id="Row2">
        <td class="Column1">text</td>
        <td class="Column2">Image</td>
    </tr>
</table>

When I hover over the column I need to show the following menu as an overlay over the first column 当我将鼠标悬停在列上时,我需要在第一列上显示以下菜单作为叠加层

<div class="drawer" style="display:none;">
   <ul class="nav nav-tabs nav-stacked">
      <li class="active"><a href="#">Products</a></li><!-- active link changes -->
      <li><a href="#">Information</a></li>
      <li><a href="#">History</a></li>
      <li><a href="#">Services</a></li>
      <li><a href="#">Orders Complete</a></li>
   </ul>
</div>

You can apply position:relative for the <tr> , position:absolute for the menu ( to take it out of normal flow, So that the table cells won't jump around ) and append the menu to the respective <tr> which is hovered. 您可以对<tr>应用position:relative ,为菜单position:absolute使其脱离正常流程,以便表格单元格不会跳转 )并将菜单附加到相应的<tr> ,即徘徊。

something like the following: 类似以下内容:

 $("table tbody tr").hover(function(event) { $(".drawer").show().appendTo($(this).find("td:first")); }, function() { $(this).find(".drawer").hide(); }); 
 tbody tr { position: relative; } .drawer { display: none; position: absolute; background:dodgerblue; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table> <thead> <tr> <th class="Column1">Column</th> <th class="Column2">Column2</th> </tr> </thead> <tbody> <tr id="Row1"> <td class="Column1">text</td> <td class="Column2">Image</td> </tr> <tr id="Row2"> <td class="Column1">text</td> <td class="Column2">Image</td> </tr> </tbody> </table> <div class="drawer"> <ul class="nav nav-tabs nav-stacked"> <li class="active"><a href="#">Products</a> </li> <!-- active link changes --> <li><a href="#">Information</a> </li> <li><a href="#">History</a> </li> <li><a href="#">Services</a> </li> <li><a href="#">Orders Complete</a> </li> </ul> </div> 


Side note: I also took the liberty to separate the heading into <thead> and data into <tbody> , and moved the inline styles to CSS. 旁注:我也冒昧地将标题分为<thead> ,将数据分成<tbody> ,并将内联样式移到CSS中。 The script will work regardless. 该脚本无论如何都会有效。

Try this, it doesn't require any changes to your existing HTML / CSS: 试试这个,它不需要对现有的HTML / CSS进行任何更改:

$('.Column1').mouseover(function(e){

  $('.drawer').css({
    'display': 'block';
    'position': 'absolute';
    'left': e.pageX;
    'top':e.pageY;
  });
}).mouseleave(function(e){
  $('.drawer').css({
    'display': 'none';
  });
});

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

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