简体   繁体   English

从 HTML 页面上的另一个元素继承宽度

[英]Inherit width from another element on HTML page

I'm working on a simple webpage which interacts with a database to retrieve flights to and from places specified by the user.我正在开发一个简单的网页,该网页与数据库交互以检索往返用户指定地点的航班。 The problem that I'm having is with displaying the flights on the front-end using HTML and CSS.我遇到的问题是使用 HTML 和 CSS 在前端显示航班。 I'm not sure if there is a better way to phrase the title of this question.我不确定是否有更好的方法来表达这个问题的标题。 I have a HTML table which I am using as a sort bar.我有一个 HTML 表用作排序栏。 The code for that is below along with the stylesheet nodes describing the tr element:下面的代码以及描述 tr 元素的样式表节点:

<tr>
    <td class="xp-flt-sortbar-label" id="flightSortByLabel">
        Sort by:
    /td>

    <td onclick="sortInPlace('1');" class="xp-flt-sortbar-col1" id="sortType1">
        <span class="flightSortShowPointer">
            Departure Time
        </span>
    </td>

    <td onclick="sortInPlace('2');" class="xp-flt-sortbar-col2" id="sortType2">
        <span class="flightSortShowPointer">
            Arrival Time
        </span>
    </td>       
</tr>

<!--Stylesheet-->
#flightResultsSortDiv .xp-flt-sortbar-col1 {
    padding-left: 6px;
    text-align: left;
    width: 100px;
}
#flightResultsSortDiv .xp-flt-sortbar-col2 {
    padding-left: 0;
    text-align: left;
    width: 128px;
}

On top of this, I have HTML to describe the data elements of the table.最重要的是,我有 HTML 来描述表的数据元素。 Below is one such snippet.下面是一个这样的片段。

<tr class="xp-flt-ctyttl-fnt" name="from2City">
    <td colspan="2" class="flightLegSummary"><span id="departureCity0">Seattle</span></td>
    <td colspan="2"><span id="arrivalCity0">Newark</span></td>
</tr>

The problem that I am facing is that I want to make the width of the Departure time column title exactly the same as the corresponding data element.我面临的问题是我想让 Departure time 列标题的宽度与相应的数据元素完全相同。 I don't think making the width of the Departure time element 100% is a viable option for me.我不认为将出发时间元素的宽度设为 100% 对我来说是一个可行的选择。 Is there a way for me to inherit the width of the data element and set the Departure time column title accordingly?有没有办法让我继承数据元素的宽度并相应地设置出发时间列标题?

If your "sort bar" and your "data elements" are in different tables, and you want the Departure Time column's width to correspond to its data cell's width, you'll have to use javascript.如果您的“排序栏”和“数据元素”位于不同的表格中,并且您希望出发时间列的宽度与其数据单元格的宽度相对应,则必须使用 javascript。 You'd do something like this:你会做这样的事情:

HTML: HTML:

<table>
<tr>
  <td id="dt">Departure Time</td>
</tr>
</table>

<table>
<tr>
  <td id="dc">data that is presumably longer than Departure Time</td>
</tr>
</table>

Javascript: Javascript:

document.getElementById("dt").width = document.getElementById("dc").offsetWidth;
/* Now "dt" is a few pixels wider than "dc", so compensate by setting "dc"'s width to "dt"'s */
document.getElementById("dc").width = document.getElementById("dt").width;

If you can only use CSS, the best you can do is enforce the same width for both cells by setting them to the same value in the CSS.如果您只能使用 CSS,您可以做的最好的事情是通过在 CSS 中将两个单元格设置为相同的值来强制两个单元格的宽度相同。 You can't use "inherit" here because the cells don't have a parent-child relationship.您不能在此处使用“继承”,因为单元格没有父子关系。

From what I understand, you have 2 columns in 2 tables, and you want them of the same width.据我了解,您在 2 个表中有 2 列,并且您希望它们具有相同的宽度。

And of course, you want them to be of the smallest width possible.当然,您希望它们的宽度尽可能小。

First option: fixed width for both columns.第一个选项:两列的固定宽度。

Second option: merge the 2 tables into one第二种选择:将 2 个表合并为一个

Third option: use Javascript to set the column width.第三个选项:使用 Javascript 设置列宽。

DepartureColTitle.width() = DepartureColData.width() DepartureColTitle.width() = DepartureColData.width()

The third option is the only way to get a width from an element in a page and apply it to another unrelated element.第三个选项是从页面中的元素获取宽度并将其应用于另一个不相关元素的唯一方法。

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

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