简体   繁体   English

使用CSS / JavaScript迭代隐藏html表的最后一列

[英]Iteratively hide the last column of a html table using css/javascript

I am attempting to modify a table created by xx user through Sharepoint with responsive design in mind. 我正在尝试通过响应点设计通过Sharepoint修改xx用户创建的表。 What I want to do is iteratively hide the last visible column in the table based on the width of the screen. 我想做的是根据屏幕的宽度迭代地隐藏表格中的最后一个可见列。

For example: a user creates a 10-column table and the max screen size is 1200. If they minimize the window to 600px, then the last 5 columns should not be shown. 例如:用户创建一个10列的表,最大屏幕尺寸为1200。如果他们将窗口最小化为600px,则不应显示最后5列。 If they minimize the window to 800px, then the last 3 columns should not be shown. 如果它们将窗口最小化为800px,则不应显示最后3列。

Currently I can selectively remove columns with CSS using display:none on the :nth-child, but that doesn't account for whether or not the display is already set to none in order to move on to the n-1-child. 目前,我可以在:nth-​​child上使用display:none使用CSS有选择地删除列,但这不能说明是否已将显示设置为none才能继续使用n-1子级。

Is there a way to do this with CSS, or is this concept limited to Javascript? 有没有办法用CSS做到这一点,还是这个概念仅限于Javascript? And if so, how? 如果是这样,怎么办?

Thank you. 谢谢。

http://jsfiddle.net/tcvB6/1/ Here's one possibility. http://jsfiddle.net/tcvB6/1/这是一种可能性。 I didn't set it to continually update if the screen size is changed, but you could add that in. I wrapped the tables in a couple of divs to help the CSS, and then added this function: 如果屏幕大小发生更改,我没有将其设置为不断更新,但是您可以添加它。我将表包装在几个div中以帮助CSS,然后添加了此功能:

$("table").each(function(){
    var $table = $(this);
    while ($table.outerWidth() > $("#wrapOuter").outerWidth()) {
        $table.find("tr").each(function(){
            $(this).find("td:visible, th:visible").last().hide();
        });
    }
});

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

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