<tbody> - the table body section
Basic Concept of the <tbody>
Tag
The <tbody>
tag is used in HTML tables to define the main content of the table. It must appear as a direct child of the <table>
element, typically following <thead>
and <tfoot>
. A table can contain multiple <tbody>
elements, which is often used in practical development to group large sets of table data.
<table>
<thead>
<tr>
<th>Name</th>
<th>Age</th>
</tr>
</thead>
<tbody>
<tr>
<td>Zhang San</td>
<td>25</td>
</tr>
<tr>
<td>Li Si</td>
<td>30</td>
</tr>
</tbody>
</table>
Syntax Features of <tbody>
The <tbody>
tag has the following syntactic characteristics:
- Must contain one or more
<tr>
elements - Can include global attributes such as
class
,id
,style
, etc. - Supports event-handling attributes like
onclick
,onmouseover
, etc. - Corresponds to the
HTMLTableSectionElement
interface in the DOM
<tbody id="userData" class="highlight" onclick="handleClick(event)">
<!-- Table row content -->
</tbody>
Application of Multiple <tbody>
Groupings
When table data requires logical grouping, multiple <tbody>
elements can be used. This structure not only enhances semantics but also facilitates operations on different data groups via CSS or JavaScript.
<table>
<thead>
<tr><th>Product</th><th>Sales</th></tr>
</thead>
<tbody>
<tr><td>Phone</td><td>1200</td></tr>
<tr><td>Tablet</td><td>800</td></tr>
</tbody>
<tbody>
<tr><td>Laptop</td><td>1500</td></tr>
<tr><td>Desktop</td><td>600</td></tr>
</tbody>
</table>
Styling and Interaction
The <tbody>
element can be styled independently, providing more possibilities for the visual presentation of tables. CSS can easily achieve zebra-striping or group highlighting effects.
/* Basic zebra-striping effect */
tbody tr:nth-child(odd) {
background-color: #f2f2f2;
}
/* Group spacing styles */
tbody {
border-bottom: 2px solid #ddd;
}
/* Hover effect */
tbody tr:hover {
background-color: #e6f7ff;
}
JavaScript Manipulation of <tbody>
JavaScript can dynamically manipulate <tbody>
content, which is common in modern data-driven web applications. Here are some typical examples:
// Get the table body
const tbody = document.querySelector('tbody');
// Add a new row
function addRow(data) {
const row = tbody.insertRow();
row.innerHTML = `<td>${data.name}</td><td>${data.age}</td>`;
}
// Delete all rows
function clearTable() {
tbody.innerHTML = '';
}
// Sort rows
function sortTable(columnIndex) {
const rows = Array.from(tbody.rows);
rows.sort((a, b) =>
a.cells[columnIndex].textContent.localeCompare(b.cells[columnIndex].textContent)
);
tbody.append(...rows);
}
<tbody>
in Responsive Design
When handling large tables on mobile devices, <tbody>
can be combined with CSS to achieve responsive layouts. Common techniques include horizontal scrolling and row-column conversion.
/* Responsive table container */
.table-container {
overflow-x: auto;
}
/* Special styles for small screens */
@media (max-width: 600px) {
tbody tr {
display: block;
margin-bottom: 1em;
border: 1px solid #ddd;
}
tbody td {
display: block;
text-align: right;
}
tbody td::before {
content: attr(data-label);
float: left;
font-weight: bold;
}
}
Accessibility Considerations
Ensuring the accessibility of <tbody>
content is crucial for users with disabilities. ARIA attributes and proper keyboard navigation are key to achieving this goal.
<table aria-label="User Data Table">
<thead>
<tr><th scope="col">Name</th><th scope="col">Email</th></tr>
</thead>
<tbody aria-live="polite">
<tr><td tabindex="0">Wang Wu</td><td tabindex="0">wang@example.com</td></tr>
</tbody>
</table>
Performance Optimization Techniques
When dealing with large datasets, performance optimization for <tbody>
is particularly important. Virtual scrolling and paginated loading are two commonly used techniques.
// Virtual scrolling example
function renderVisibleRows() {
const scrollTop = tableContainer.scrollTop;
const startIdx = Math.floor(scrollTop / rowHeight);
const endIdx = startIdx + visibleRowCount;
tbody.style.height = `${data.length * rowHeight}px`;
tbody.style.transform = `translateY(${startIdx * rowHeight}px)`;
const fragment = document.createDocumentFragment();
for (let i = startIdx; i <= endIdx; i++) {
if (data[i]) {
const row = createRow(data[i]);
fragment.appendChild(row);
}
}
tbody.innerHTML = '';
tbody.appendChild(fragment);
}
Collaboration with Other Table Elements
<tbody>
needs to work in conjunction with other table elements like <thead>
, <tfoot>
, and <caption>
to form a complete table structure.
<table>
<caption>2023 Sales Report</caption>
<thead>
<tr><th>Quarter</th><th>Sales</th></tr>
</thead>
<tfoot>
<tr><td>Total</td><td>$1,200,000</td></tr>
</tfoot>
<tbody>
<tr><td>Q1</td><td>$250,000</td></tr>
<tr><td>Q2</td><td>$300,000</td></tr>
</tbody>
<tbody>
<tr><td>Q3</td><td>$350,000</td></tr>
<tr><td>Q4</td><td>$300,000</td></tr>
</tbody>
</table>
Usage in Modern Frameworks
In modern frontend frameworks like React and Vue, the usage of <tbody>
differs slightly, often combined with data binding and componentization.
// React example
function DataTable({ data }) {
return (
<table>
<thead>
<tr>
<th>ID</th>
<th>Name</th>
</tr>
</thead>
<tbody>
{data.map(item => (
<tr key={item.id}>
<td>{item.id}</td>
<td>{item.name}</td>
</tr>
))}
</tbody>
</table>
);
}
Browser Rendering Differences
Different browsers have subtle variations in rendering <tbody>
, particularly when handling borders, background colors, and dynamic content.
/* Addressing browser compatibility issues */
tbody {
/* Ensure consistent background color rendering in Firefox */
background-color: inherit;
/* Fix border issues in IE */
border-collapse: separate;
border-spacing: 0;
}
Print Style Optimization
For printing scenarios, <tbody>
may require special styling to ensure readability on paper media.
@media print {
tbody tr {
page-break-inside: avoid;
}
tbody tr:nth-child(odd) {
background-color: #fff !important;
-webkit-print-color-adjust: exact;
}
table {
border-top: 2pt solid black;
}
}
本站部分内容来自互联网,一切版权均归源网站或源作者所有。
如果侵犯了你的权益请来信告知我们删除。邮箱:cc@cccx.cn
上一篇:-表头部分
下一篇:<tfoot>-表尾部分 Front End Chuan, Chen Chuan's Code Teahouse 🍵, specializing in exorcising all kinds of stubborn bugs 💻. Daily serving baldness-warning-level development insights 🛠️, with a bonus of one-liners that'll make you laugh for ten years 🐟. Occasionally drops pixel-perfect romance brewed in a coffee cup ☕.
Front End Chuan
相关文章