简体   繁体   English

鼠标单击时如何使动态表可滚动?

[英]How to make my dynamic table scrollable when mouse clicked?

I have a html table: 我有一个html表:

<table id="my-table" class="my-table">

        <tr class="head">                                
               <th class="name">Name:</th>
               <th class="age">Age:</th>
        </tr>

        <tr class="row">
               <td class="name">John</td>
               <td class="age">19</td>
        </tr class="row">

    <tr class="row">
               <td class="name">Kate</td>
               <td class="age">16</td>
        </tr>
       ...
       ...

  </table>

The table can have several rows which fits 100px height area. 表格可以有几行,适合100px的高度区域。

Then, I defined a mouse click event, that's when mouse click on each row's name column, there will be some content be appended after the clicked row: 然后,我定义了一个鼠标单击事件,即当鼠标单击每行的名称列时,将在单击的行后附加一些内容:

function addContent(evt){
   $('.row').after("<tr>"+SOME_CONTENT+"</tr>");
}

$(".name").click(addContent);

It works and the above click event could make the table much longer, since extra content row are appended after each row if mouse clicked. 它可以正常工作,并且上述click事件可能会使表更长,因为如果单击鼠标,则会在每行之后附加额外的内容行。

My Question is , how to make the table scrollable (scroll bar) when mouse clicked on the "name" column? 我的问题是 ,当鼠标单击“名称”列时如何使表格可滚动(滚动条)? (That's by default, not scrollable, only when mouse clicked on "name" which triggered extra content appended, then makes it scrollable), so that the table area has the fixed height 100 px always. (默认情况下,这是不可滚动的,仅当鼠标单击“名称”时会触发附加内容的附加内容,然后使其可滚动),以便表区域始终具有固定的高度100 px。

I tried in CSS: 我在CSS中尝试过:

.my-table{  
 overflow:scroll;
 height: 100px;
 width: 600px;
 overflow:auto;

}

but it does not work... 但这不起作用...

With Jquery, you could add 使用Jquery,您可以添加

$(".my-table").css('overflow','hidden')

and when you want it to be scrollable (with a javascript event) 并且当您希望它可滚动时(带有javascript事件)

$(".my-table").css('overflow','scroll')

table elements don't overflow, but you could use a wrapper surrounding the table and add your css to that: table元素不会溢出,但是您可以在表格周围使用包装器,并在其中添加CSS:

http://jsfiddle.net/2ezdx/1/ http://jsfiddle.net/2ezdx/1/

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

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