表头/正文宽度问题

发布于 2024-11-07 22:58:18 字数 269 浏览 0 评论 0原文

我有一个表格,其中有标题行和正文内容的几行。

现在这些实际上是两个单独的表:

  1. 标题
  2. 对于正文内容的

(它是另一个 iframe 的一部分),我希望列标题及其相应的正文内容的宽度应该相同。

如何确保两个宽度相似并且外观相似,就好像它们是同一张表的一部分一样?

我对 CSS 或 Javascript 修复持开放态度。

注意:我将无法使用固定的列宽度,而且我也无法控制合并到一个表中...

I have a table which has a header row and few rows for the body content.

Now these are actually 2 separate tables:

  1. for Header
  2. for Body content (which is part of another iframe)

I want that the widths should be same for the column header and it's corresponding body content.

How can I ensure that both the widths are similar and the appearance would be similar as if they are part of the same table?

I am open to both CSS or Javascript fix..

Note: I'll not be able to use fixed col widths AND neither do I have control to merge into one table...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

只为一人 2024-11-14 22:58:18

也许是这样的?这将使你的表表现得好像在那里一个

    var table1 = document.getElementsByTagName('table')[0].getElementsByTagName('tr');
    var table2 = document.getElementsByTagName('iframe')[0].contentDocument.getElementsByTagName('tr');

    var tr = merge([table1,table2]);
    var padding = 2; //set your padding to make up for difference from offsetwidth vs style width

    var higestWidth = new Array();
    for(var i in tr){
        for(var j in tr[i].childNodes){
        var td = tr[i].childNodes[j];
        if(typeof td == 'object'){
            if(higestWidth[td.cellIndex] == null || higestWidth[td.cellIndex] < td.offsetWidth){
            higestWidth[td.cellIndex] = td.offsetWidth;
            }
        }
        }
        for(var j in tr[i].childNodes){
        var td = tr[i].childNodes[j];
        if(typeof td == 'object'){
            td.setAttribute('width', higestWidth[td.cellIndex] - padding);
            }
        }
        }

几乎忘记我在这里使用我自己的一些功能,这里也来了

        var foreach = function(object,loop){
        for (var key in object) {
            if (object.hasOwnProperty(key)) {
            loop(object[key],key);
            }
        }
    }
    var merge = function(objectCollections){
        var array = new Array();
        foreach(objectCollections,function(objectCollection){
            foreach(objectCollection,function(object){
                array.push(object);
            });
        });
        return array;
    }

Maybe something like this? This will make your tables act as if thay where one

    var table1 = document.getElementsByTagName('table')[0].getElementsByTagName('tr');
    var table2 = document.getElementsByTagName('iframe')[0].contentDocument.getElementsByTagName('tr');

    var tr = merge([table1,table2]);
    var padding = 2; //set your padding to make up for difference from offsetwidth vs style width

    var higestWidth = new Array();
    for(var i in tr){
        for(var j in tr[i].childNodes){
        var td = tr[i].childNodes[j];
        if(typeof td == 'object'){
            if(higestWidth[td.cellIndex] == null || higestWidth[td.cellIndex] < td.offsetWidth){
            higestWidth[td.cellIndex] = td.offsetWidth;
            }
        }
        }
        for(var j in tr[i].childNodes){
        var td = tr[i].childNodes[j];
        if(typeof td == 'object'){
            td.setAttribute('width', higestWidth[td.cellIndex] - padding);
            }
        }
        }

Almost forgot im using some of my own functions here, here thay come aswell

        var foreach = function(object,loop){
        for (var key in object) {
            if (object.hasOwnProperty(key)) {
            loop(object[key],key);
            }
        }
    }
    var merge = function(objectCollections){
        var array = new Array();
        foreach(objectCollections,function(objectCollection){
            foreach(objectCollection,function(object){
                array.push(object);
            });
        });
        return array;
    }
妖妓 2024-11-14 22:58:18

我有点同意罗德里戈的观点,尽管我会设置最大宽度和最小宽度,而不仅仅是像这样的宽度:

.col-username { 
     max-width:150px;
     min-width:150px;
}
.col-email { 
     max-width:200px;
     min-width:200px;
}

否则,实际大小可能取决于表格单元格内容和可用空间。

I sort of agree with Rodrigo, although I would set max-width and min-width, rather than just width like so:

.col-username { 
     max-width:150px;
     min-width:150px;
}
.col-email { 
     max-width:200px;
     min-width:200px;
}

Otherwise, the actual size can depend on the table cell content and available space.

遗弃M 2024-11-14 22:58:18

如果表格的“流动性”并不重要,您可以为每个列定义类并使用 css 设置它们的样式。例如:

.col-username { width:150px; }
.col-email { width:200px; }

等等。

编辑:这里是我刚刚发现的一个例子,它使用CSS来实现这种效果。

If it is not important that the table be "fluid", you can define classes for each of your columns and style them with css. Ex:

.col-username { width:150px; }
.col-email { width:200px; }

and so on.

Edit: here is an example I just found that uses CSS to achieve this effect.

川水往事 2024-11-14 22:58:18

如果可以的话,您应该使用

<table>
  <thead>
    <tr>
      <th>Col 1</th>
      <th>Col 2</th>
      <th>Col 3</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Row 1 / Col 1</td>
      <td>Row 1 / Col 2</td>
      <td>Row 1 / Col 3</td>
    </tr>
    <tr>
      <td>Row 2 / Col 1</td>
      <td>Row 2 / Col 2</td>
      <td>Row 2 / Col 3</td>
    </tr>
    ...
  </tbody>
</table>

除非有特殊原因需要单独的表,否则这样做会简单得多。

You should be using <thead> and <tbody> if you can.

<table>
  <thead>
    <tr>
      <th>Col 1</th>
      <th>Col 2</th>
      <th>Col 3</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Row 1 / Col 1</td>
      <td>Row 1 / Col 2</td>
      <td>Row 1 / Col 3</td>
    </tr>
    <tr>
      <td>Row 2 / Col 1</td>
      <td>Row 2 / Col 2</td>
      <td>Row 2 / Col 3</td>
    </tr>
    ...
  </tbody>
</table>

Unless there's a reason you have separate tables, it would be much simpler to do it this way.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文