简体   繁体   English

为什么在动态添加包含div的行时,我的HTML表列会重新调整大小?

[英]Why do my HTML table columns resize when I dynamically add a row containing a div?

I am trying to add a table row containing a div dynamically using JavaScript. 我试图使用JavaScript动态添加包含div的表行。 Everything is working fine except, when it is added, my columns move slightly. 一切都工作得很好,除了,当它添加,我的列稍微移动。 Weirdly, it seems to only happen when the div contains text longer than a certain length. 奇怪的是,它似乎只发生在div包含长度超过一定长度的文本时。

In the stripped down version below, you can see the problem clearly. 在下面的精简版中,您可以清楚地看到问题。 After trying it, go to line 24 of the JavaScript, remove the "i" at the end of the string, and it will no longer move my columns. 尝试之后,转到JavaScript的第24行,删除字符串末尾的“i”,它将不再移动我的列。

JSFiddle 的jsfiddle

 setEventListeners(); function setEventListeners() { var hideMe = document.getElementById('hide-me'); var table = document.getElementById('table'); hideMe.addEventListener('mouseenter', showHoverMenu); table.addEventListener('mouseleave', deleteOtherMenus); } function showHoverMenu(e) { e.preventDefault(); deleteOtherMenus(); var tr = document.createElement('tr'); tr.setAttribute('class', 'row-menu-parent') var td = document.createElement('td'); td.colSpan = 4; var rowMenu = document.createElement('div'); rowMenu.classList.add('row-menu'); var div = document.createElement('div'); // Delete the "i" at the end of the string and try hovering again div.innerHTML = 'abcdefghi'; rowMenu.appendChild(div); td.appendChild(rowMenu); tr.appendChild(td); var target = e.currentTarget; target.parentNode.insertBefore(tr, target.nextSibling); } function deleteOtherMenus() { var rowMenu = document.getElementsByClassName('row-menu-parent'); if (rowMenu.length > 0) { rowMenu[0].parentNode.removeChild(rowMenu[0]); } } 
 * { border: 1px solid red; } table { width: 100%; } .row-menu div { background-color: lightGrey; } 
 <table id="table"> <tr id="hide-me"> <td>A</td> <td>B</td> <td>C</td> <td>D</td> </tr> </table> 

What is causing my columns to move, and how do I fix it? 是什么导致我的列移动,我该如何解决它?

EDIT: The columns need to be able to automatically resize, so a fixed table layout will not work. 编辑:列需要能够自动调整大小,因此固定的表格布局将无法正常工作。

You can set your table to table-layout: fixed; 您可以将表设置为table-layout: fixed; if you don't want it resizing. 如果你不想要它调整大小。

table {
    table-layout: fixed; /* Add this */
    width: 100%;
}

 setEventListeners(); function setEventListeners() { var hideMe = document.getElementById('hide-me'); var table = document.getElementById('table'); hideMe.addEventListener('mouseenter', showHoverMenu); table.addEventListener('mouseleave', deleteOtherMenus); } function showHoverMenu(e) { e.preventDefault(); deleteOtherMenus(); var tr = document.createElement('tr'); tr.setAttribute('class', 'row-menu-parent') var td = document.createElement('td'); td.colSpan = 4; var rowMenu = document.createElement('div'); rowMenu.classList.add('row-menu'); var div = document.createElement('div'); // Delete the "i" at the end of the string and try hovering again div.innerHTML = 'abcdefghi'; rowMenu.appendChild(div); td.appendChild(rowMenu); tr.appendChild(td); var target = e.currentTarget; target.parentNode.insertBefore(tr, target.nextSibling); } function deleteOtherMenus() { var rowMenu = document.getElementsByClassName('row-menu-parent'); if (rowMenu.length > 0) { rowMenu[0].parentNode.removeChild(rowMenu[0]); } } 
 * { border: 1px solid red; } table { table-layout: fixed; width: 100%; } .row-menu div { background-color: lightGrey; } 
 <table id="table"> <tr id="hide-me"> <td>A</td> <td>B</td> <td>C</td> <td>D</td> </tr> </table> 

Try setting box-sizing: border-box in CSS for the table and/or wrap the .row-menu div in a div that does not have any styling. 尝试在CSS中为表设置box-sizing: border-box和/或将.row-menu div包装在没有任何样式的div中。

If you look closely, the inner div's border is expanding the size of the parent TD which is causing the table to re-adjust. 仔细观察,内部div的边界正在扩大父TD的大小,导致表重新调整。

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

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