简体   繁体   English

根据表格变量宽度设置DIV的宽度

[英]Set the width of a DIV according to table variable width

I have two tables that are related between them but are in a different scoopes and I sometimes need to hide some columns and make these 2 tables the same column width. 我有两个相互关联的表,但它们位于不同的勺中,有时我需要隐藏一些列并使这两个表具有相同的列宽。 I already achieve that setting the min-width and max-width to the table but above the table I have a separated panel that must have the same width of the table. 我已经实现了将最小宽度和最大宽度设置为表格的方法,但是在表格上方,我有一个单独的面板,必须具有与表格相同的宽度。

So, my tables can have min-width:500px and max-width:700px according to the validations. 因此,根据验证,我的表可以具有min-width:500px和max-width:700px。

How can I set the same current size to the div of above. 如何将相同的当前大小设置为上述div。

I tried to define the window.onload like this. 我试图这样定义window.onload。

<script type="text/javascript">
window.onload = ($('[id$=pnlActions]')[0]).clientWidth = document.getElementById('myTable').offsetWidth;

Or this 或这个

window.onload = ($('[id$=pnlActions]')[0]).style.clienWidth = document.getElementById('myTable').offsetWidth;

And this 和这个

window.onload = ($('[id$=pnlActions]')[0]).style.offsetWidth = document.getElementById('myTable').offsetWidth;
</script>

Nothing happens 什么都没发生

<asp:Panel ID="pnlActions" runat="server" style="min-width:500px; max-width:700px;">
</asp:Panel>
<table id="myTable" cellpadding="0" cellspacing="0" style="min-width:500px; max-width:700px;">
</table>
<table id="myTable2" cellpadding="0" cellspacing="0" style="min-width:500px; max-width:700px;">
</table>

Even instead of a panel, I used a client div and didn't work at all. 我什至没有使用面板,而是使用了一个客户div,并且根本没有工作。

<div id="divActions" style="min-width:500px; max-width:700px;">
</div>

window.onload = document.getElementById('divActions').clientWidth = document.getElementById('myTable').offsetWidth;

I tested this on Chrome 29, IE8 and IE10 我在Chrome 29,IE8和IE10上进行了测试

To begin with I don think style is a valid ASP tag, 首先,我认为样式不是有效的ASP标签,

<asp:Panel ID="pnlActions" runat="server" style="min-width:500px; max-width:700px;">

Have you tried making a class and using CssClass instead? 您是否尝试过制作一个类并改用CssClass? Just to be sure! 只是要确定!

Edit: 编辑:

I made a fiddle for you where it works fine http://jsfiddle.net/VYLsE/2/ 我为您制作了一个小提琴,在它可以正常工作的地方http://jsfiddle.net/VYLsE/2/

$(function () {
    var width = $('#myTable').width();
    $('#divActions').width(width);
});

Remember you have to load jQuery on your page too! 请记住,您也必须在页面上加载jQuery!

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

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