简体   繁体   English

使用Java脚本更改表结构

[英]Change table structure with Javascript

I have a (left) menu sidebar that I'd like to allow users to toggle on off. 我有一个(左侧)菜单侧栏,我希望允许用户将其打开。 Have code to make it drop down and collapse up, leaving blankspace when collapsed However, it may look better for site to expand content in adjacent pane over to fill in where menu was. 有使它下降并折叠的代码,但在折叠时留有空白。但是,站点在相邻窗格中扩展内容以填充菜单所在的位置看起来更好。 Right now menu is in single cell of table and content is in single cell to right, both in same row. 现在,菜单位于表格的单个单元格中,内容位于右侧的单个单元格中,均位于同一行中。 I'd like to avoid changing number of cells in row as that would conflict with rows above and below. 我想避免更改行中的单元格数量,因为这将与上方和下方的行发生冲突。 So one approach would be to toggle between having two cells incl menu and one cell with content but colspan of two. 因此,一种方法是在具有两个单元格(包括菜单)和一个具有内容但相邻两个的单元格之间切换。 My understanding is that Javascript dropdowns work by preloading both versions and then hiding one. 我的理解是Javascript下拉菜单通过预加载两个版本然后隐藏一个版本来工作。 Would it be possible to make the Javascript change the table structure? 有可能使Javascript更改表结构吗? Imagine it would look something like below but this does not, of itself, work. 想象一下,它看起来像下面的样子,但这本身不起作用。

<script type="text/javascript">
        function toggleBar(obj) {
            var navbar = document.getElementById("navbar");
            if (navbar.style.display == "none") {
                navbar.style.display = "";
                obj.innerHTML = "<img src='images/collapse.gif' alt='Hide Menu'>";
            } else {
                navbar.style.display = "none";
                obj.innerHTML = "<img src='images/expand.gif' alt='Show Menu'><td colspan=2";
            }
        }

and in body: 在身体上:

<table><tr><div style="background-color:silver;width:100px">
<a href="javascript:void(0)" onclick="toggleBar(this);"><td><img src="images/collapse.gif" alt="Hide Menu"><div id="navbar" ><a href="javascript:void(0)" onclick="toggleBar(this);"></a></a>menu here<br>menu1<br>menu2</td><td></div></div>CONTENT GOES HERE<td></tr>
<table>

This is tricky .. I suppose it's a bit late to say "Don't use tables for layout!" 这很棘手..我想说“不要使用表格进行布局!”有点晚了 , but that is essentially your problem. ,但这实际上是您的问题。

If the table is connected to anything below it, and you need to use colspan then I don't see an easy way to do this .. 如果表连接到它下面的任何东西,并且您需要使用colspan,那么我看不到实现此目的的简便方法。

I suppose you could add new row, with n-1 columns and the colspan, then copy all the HTML over into the new cell and delete the old row. 我想您可以添加具有n-1列和colspan的新行,然后将所有HTML复制到新单元格中并删除旧行。 But that would break any JavaScript you might on original cell. 但这会破坏您在原始单元格上可能出现的所有JavaScript。

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

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