使用javascript

时间:2018-08-06 08:17:31

标签: javascript css css-tables

我的HTML页面有2个表:

  • 第一个表,其中有class='af_column_column-header-table' and id ='Table1'`。
  • 第二张表。

css:

.af_column_column-header-table { 
     width = 100% !important;
 }

问题:

  • 如果第二个表有多个行,则 将第一张桌子的宽度设置为99.3%。
  • 我只想在第一个表中这样做。

    PS:我的页面上有很多使用相同样式类的表 af_column_column-header-table

1 个答案:

答案 0 :(得分:1)

您可以在querySelectorAll的帮助下使用:first-child选择第一个表格,然后使用.style.width设置样式:

var rows_count = document.querySelectorAll('table.af_column_column-header-table:first-child tr').length;

if( rows_count > 1){
  document.getElementById('Table1').style.width = '99.3%';
}
  

我的页面上有很多表,它们使用相同的样式类af_column_column-header-table,我只想对第一个表执行此操作。

使用:first-child选择器将达到目的。