简体   繁体   English

将输入宽度绑定到表格列宽度

[英]Bind input width to table column width

There is a table with 5 columns. 有一个包含5列的表格。 Columns might have different width (I don't assign constant width). 列的宽度可能不同(我没有分配恒定的宽度)。

Now I want to create 5 input elements just on top of the table . 现在,我想在表格顶部创建5个输入元素。 Is there any trick to make width of each input element equal to corresponding column width? 是否有任何技巧可以使每个输入元素的宽度等于相应的列宽?

For instance, Input 1 should have the width of Column 1, Input 2 - the width of Column 2, etc. 例如,输入1的宽度应为列1的宽度,输入2的宽度应为列2的宽度,依此类推。

So, how to bind inputs to columns? 那么,如何将输入绑定到列?

UPDATE: 更新:

<script>
$('#addButton').click(function() {
    $("#addContent").slideToggle("slow");
    $('input').width(+$('table td').width()-1);
});
</script>

<div id = "add">
    <div id = "addButton" title='New Record' ;>
        <img src='images/add.png' alt='New Record' />
    </div>

    <div id = "addContent">
                        <input type="text">
                        <input type="text">
    </div>
</div>

                <table id="newspaper-b" border="0" cellspacing="2" cellpadding="2" width = "100%">
                    <thead>
                        <tr>
                            <th scope="col">Opr</th>
                            <th scope="col">Flt Num</th>
                            <th scope="col"></th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($result1 as $row):
                            $status=$row['status'];
                            $airlineName=$row['airlineName'];
                            $flightNum=$row['flightNum'];
                        ?>
                        <tr id="<?php echo $flightNum; ?>" class="edit_tr">
                            <td width="80" ><?php echo $airlineName;?></td>
                            <td width="80" ><?php echo $flightNum;?></td>
                            <td width="80" id="<?php echo $flightNum; ?>" class="deact_but">
                                 <div>
                                    <img src='images/delete.png' alt='Deactivate' />
                                </div>              
                            </td>
                        </tr>
                        <?php endforeach;?>
                    </tbody>
                </table>    

CSS 的CSS

input{
    float: left;
    margin: 0;
    padding: 0;
}

It's possible with jQuery. jQuery是可能的。 Check the demo - http://jsfiddle.net/k9MhG/4/ 查看演示-http: //jsfiddle.net/k9MhG/4/

Add some JavaScript (at the end or after the page loads) to get the offsetWidth of the top level cells and apply those widths to your inputs. 添加一些JavaScript(在页面加载的最后或之后),以获取顶级单元格的offsetWidth并将这些宽度应用于您的输入。

Something like this: 像这样:

document.getElementById('myInput').style.width = document.getElementById('myTable').rows[0].cells[0].offsetWidth;

Or, extend your table to include the inputs and set the width to 100%. 或者,扩展表以包括输入并将宽度设置为100%。 You can apply styles to the first TR to make it look like it's not really part of the table. 您可以将样式应用于第一个TR,以使其看起来似乎不是表的一部分。 The next TR can contain your headers with borders, etc. 下一个TR可以包含带边框的标题等。

<table>
    <tr>
      <td><input type="text"></td>
      <td><input type="text"></td>
      <td><input type="text"></td>
    </tr>
    <tr>
      <td><br/><br/><br/></td>
    </tr>
    <tr>
        <td>ContentContent</td>
        <td>Content</td>
        <td>Cont</td>
    </tr>
</table>

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

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