Hướng dẫn which of the following elements can be used in html to create a table? - Phần tử nào sau đây có thể được sử dụng trong html để tạo bảng?

tags and opening and closing table data tags are used to create a row of data.

Những yếu tố nào có thể chứa bảng HTML?

Lưu ý: Một ô bảng có thể chứa tất cả các loại phần tử HTML: văn bản, hình ảnh, danh sách, liên kết, các bảng khác, v.v.

Phần tử bảng trong HTML là gì?

: Phần tử bảng.Phần tử HTML đại diện cho dữ liệu bảng-nghĩa là thông tin được trình bày trong bảng hai chiều bao gồm các hàng và cột của các ô chứa dữ liệu.represents tabular data — that is, information presented in a two-dimensional table comprised of rows and columns of cells containing data.

Phần tử HTML ____10 đại diện cho dữ liệu bảng-nghĩa là thông tin được trình bày trong bảng hai chiều bao gồm các hàng và cột của các ô chứa dữ liệu.

<p>Simple table with headerp>
<table>
  <tr>
    <th>First nameth>
    <th>Last nameth>
  tr>
  <tr>
    <td>Johntd>
    <td>Doetd>
  tr>
  <tr>
    <td>Janetd>
    <td>Doetd>
  tr>
table>

<p>Table with thead, tfoot, and tbodyp>
<table>
  <thead>
    <tr>
      <th>Header content 1th>
      <th>Header content 2th>
    tr>
  thead>
  <tbody>
    <tr>
      <td>Body content 1td>
      <td>Body content 2td>
    tr>
  tbody>
  <tfoot>
    <tr>
      <td>Footer content 1td>
      <td>Footer content 2td>
    tr>
  tfoot>
table>

<p>Table with colgroupp>
<table>
  <colgroup span="4">colgroup>
  <tr>
    <th>Countriesth>
    <th>Capitalsth>
    <th>Populationth>
    <th>Languageth>
  tr>
  <tr>
    <td>USAtd>
    <td>Washington, D.C.td>
    <td>309 milliontd>
    <td>Englishtd>
  tr>
  <tr>
    <td>Swedentd>
    <td>Stockholmtd>
    <td>9 milliontd>
    <td>Swedishtd>
  tr>
table>

<p>Table with colgroup and colp>
<table>
  <colgroup>
    <col style="background-color: #0f0" />
    <col span="2" />
  colgroup>
  <tr>
    <th>Limeth>
    <th>Lemonth>
    <th>Orangeth>
  tr>
  <tr>
    <td>Greentd>
    <td>Yellowtd>
    <td>Orangetd>
  tr>
table>

<p>Simple table with captionp>
<table>
  <caption>
    Awesome caption
  caption>
  <tr>
    <td>Awesome datatd>
  tr>
table>
0 HTML element represents tabular data — that is, information presented in a two-dimensional table comprised of rows and columns of cells containing data.

Show

Thử nó

Thuộc tính

Yếu tố này bao gồm các thuộc tính toàn cầu.

Thuộc tính không dùng nữa

<p>Simple table with headerp>
<table>
  <tr>
    <th>First nameth>
    <th>Last nameth>
  tr>
  <tr>
    <td>Johntd>
    <td>Doetd>
  tr>
  <tr>
    <td>Janetd>
    <td>Doetd>
  tr>
table>

<p>Table with thead, tfoot, and tbodyp>
<table>
  <thead>
    <tr>
      <th>Header content 1th>
      <th>Header content 2th>
    tr>
  thead>
  <tbody>
    <tr>
      <td>Body content 1td>
      <td>Body content 2td>
    tr>
  tbody>
  <tfoot>
    <tr>
      <td>Footer content 1td>
      <td>Footer content 2td>
    tr>
  tfoot>
table>

<p>Table with colgroupp>
<table>
  <colgroup span="4">colgroup>
  <tr>
    <th>Countriesth>
    <th>Capitalsth>
    <th>Populationth>
    <th>Languageth>
  tr>
  <tr>
    <td>USAtd>
    <td>Washington, D.C.td>
    <td>309 milliontd>
    <td>Englishtd>
  tr>
  <tr>
    <td>Swedentd>
    <td>Stockholmtd>
    <td>9 milliontd>
    <td>Swedishtd>
  tr>
table>

<p>Table with colgroup and colp>
<table>
  <colgroup>
    <col style="background-color: #0f0" />
    <col span="2" />
  colgroup>
  <tr>
    <th>Limeth>
    <th>Lemonth>
    <th>Orangeth>
  tr>
  <tr>
    <td>Greentd>
    <td>Yellowtd>
    <td>Orangetd>
  tr>
table>

<p>Simple table with captionp>
<table>
  <caption>
    Awesome caption
  caption>
  <tr>
    <td>Awesome datatd>
  tr>
table>
1Deprecated Deprecated

Thuộc tính được liệt kê này cho biết cách bảng phải được căn chỉnh bên trong tài liệu chứa. Nó có thể có các giá trị sau:

  • <p>Simple table with headerp>
    <table>
      <tr>
        <th>First nameth>
        <th>Last nameth>
      tr>
      <tr>
        <td>Johntd>
        <td>Doetd>
      tr>
      <tr>
        <td>Janetd>
        <td>Doetd>
      tr>
    table>
    
    <p>Table with thead, tfoot, and tbodyp>
    <table>
      <thead>
        <tr>
          <th>Header content 1th>
          <th>Header content 2th>
        tr>
      thead>
      <tbody>
        <tr>
          <td>Body content 1td>
          <td>Body content 2td>
        tr>
      tbody>
      <tfoot>
        <tr>
          <td>Footer content 1td>
          <td>Footer content 2td>
        tr>
      tfoot>
    table>
    
    <p>Table with colgroupp>
    <table>
      <colgroup span="4">colgroup>
      <tr>
        <th>Countriesth>
        <th>Capitalsth>
        <th>Populationth>
        <th>Languageth>
      tr>
      <tr>
        <td>USAtd>
        <td>Washington, D.C.td>
        <td>309 milliontd>
        <td>Englishtd>
      tr>
      <tr>
        <td>Swedentd>
        <td>Stockholmtd>
        <td>9 milliontd>
        <td>Swedishtd>
      tr>
    table>
    
    <p>Table with colgroup and colp>
    <table>
      <colgroup>
        <col style="background-color: #0f0" />
        <col span="2" />
      colgroup>
      <tr>
        <th>Limeth>
        <th>Lemonth>
        <th>Orangeth>
      tr>
      <tr>
        <td>Greentd>
        <td>Yellowtd>
        <td>Orangetd>
      tr>
    table>
    
    <p>Simple table with captionp>
    <table>
      <caption>
        Awesome caption
      caption>
      <tr>
        <td>Awesome datatd>
      tr>
    table>
    
    2: Bảng được hiển thị ở phía bên trái của tài liệu;
  • <p>Simple table with headerp>
    <table>
      <tr>
        <th>First nameth>
        <th>Last nameth>
      tr>
      <tr>
        <td>Johntd>
        <td>Doetd>
      tr>
      <tr>
        <td>Janetd>
        <td>Doetd>
      tr>
    table>
    
    <p>Table with thead, tfoot, and tbodyp>
    <table>
      <thead>
        <tr>
          <th>Header content 1th>
          <th>Header content 2th>
        tr>
      thead>
      <tbody>
        <tr>
          <td>Body content 1td>
          <td>Body content 2td>
        tr>
      tbody>
      <tfoot>
        <tr>
          <td>Footer content 1td>
          <td>Footer content 2td>
        tr>
      tfoot>
    table>
    
    <p>Table with colgroupp>
    <table>
      <colgroup span="4">colgroup>
      <tr>
        <th>Countriesth>
        <th>Capitalsth>
        <th>Populationth>
        <th>Languageth>
      tr>
      <tr>
        <td>USAtd>
        <td>Washington, D.C.td>
        <td>309 milliontd>
        <td>Englishtd>
      tr>
      <tr>
        <td>Swedentd>
        <td>Stockholmtd>
        <td>9 milliontd>
        <td>Swedishtd>
      tr>
    table>
    
    <p>Table with colgroup and colp>
    <table>
      <colgroup>
        <col style="background-color: #0f0" />
        <col span="2" />
      colgroup>
      <tr>
        <th>Limeth>
        <th>Lemonth>
        <th>Orangeth>
      tr>
      <tr>
        <td>Greentd>
        <td>Yellowtd>
        <td>Orangetd>
      tr>
    table>
    
    <p>Simple table with captionp>
    <table>
      <caption>
        Awesome caption
      caption>
      <tr>
        <td>Awesome datatd>
      tr>
    table>
    
    3: Bảng được hiển thị ở trung tâm của tài liệu;
  • <p>Simple table with headerp>
    <table>
      <tr>
        <th>First nameth>
        <th>Last nameth>
      tr>
      <tr>
        <td>Johntd>
        <td>Doetd>
      tr>
      <tr>
        <td>Janetd>
        <td>Doetd>
      tr>
    table>
    
    <p>Table with thead, tfoot, and tbodyp>
    <table>
      <thead>
        <tr>
          <th>Header content 1th>
          <th>Header content 2th>
        tr>
      thead>
      <tbody>
        <tr>
          <td>Body content 1td>
          <td>Body content 2td>
        tr>
      tbody>
      <tfoot>
        <tr>
          <td>Footer content 1td>
          <td>Footer content 2td>
        tr>
      tfoot>
    table>
    
    <p>Table with colgroupp>
    <table>
      <colgroup span="4">colgroup>
      <tr>
        <th>Countriesth>
        <th>Capitalsth>
        <th>Populationth>
        <th>Languageth>
      tr>
      <tr>
        <td>USAtd>
        <td>Washington, D.C.td>
        <td>309 milliontd>
        <td>Englishtd>
      tr>
      <tr>
        <td>Swedentd>
        <td>Stockholmtd>
        <td>9 milliontd>
        <td>Swedishtd>
      tr>
    table>
    
    <p>Table with colgroup and colp>
    <table>
      <colgroup>
        <col style="background-color: #0f0" />
        <col span="2" />
      colgroup>
      <tr>
        <th>Limeth>
        <th>Lemonth>
        <th>Orangeth>
      tr>
      <tr>
        <td>Greentd>
        <td>Yellowtd>
        <td>Orangetd>
      tr>
    table>
    
    <p>Simple table with captionp>
    <table>
      <caption>
        Awesome caption
      caption>
      <tr>
        <td>Awesome datatd>
      tr>
    table>
    
    4: Bảng được hiển thị ở phía bên phải của tài liệu.

Đặt

<p>Simple table with headerp>
<table>
  <tr>
    <th>First nameth>
    <th>Last nameth>
  tr>
  <tr>
    <td>Johntd>
    <td>Doetd>
  tr>
  <tr>
    <td>Janetd>
    <td>Doetd>
  tr>
table>

<p>Table with thead, tfoot, and tbodyp>
<table>
  <thead>
    <tr>
      <th>Header content 1th>
      <th>Header content 2th>
    tr>
  thead>
  <tbody>
    <tr>
      <td>Body content 1td>
      <td>Body content 2td>
    tr>
  tbody>
  <tfoot>
    <tr>
      <td>Footer content 1td>
      <td>Footer content 2td>
    tr>
  tfoot>
table>

<p>Table with colgroupp>
<table>
  <colgroup span="4">colgroup>
  <tr>
    <th>Countriesth>
    <th>Capitalsth>
    <th>Populationth>
    <th>Languageth>
  tr>
  <tr>
    <td>USAtd>
    <td>Washington, D.C.td>
    <td>309 milliontd>
    <td>Englishtd>
  tr>
  <tr>
    <td>Swedentd>
    <td>Stockholmtd>
    <td>9 milliontd>
    <td>Swedishtd>
  tr>
table>

<p>Table with colgroup and colp>
<table>
  <colgroup>
    <col style="background-color: #0f0" />
    <col span="2" />
  colgroup>
  <tr>
    <th>Limeth>
    <th>Lemonth>
    <th>Orangeth>
  tr>
  <tr>
    <td>Greentd>
    <td>Yellowtd>
    <td>Orangetd>
  tr>
table>

<p>Simple table with captionp>
<table>
  <caption>
    Awesome caption
  caption>
  <tr>
    <td>Awesome datatd>
  tr>
table>
5 và
<p>Simple table with headerp>
<table>
  <tr>
    <th>First nameth>
    <th>Last nameth>
  tr>
  <tr>
    <td>Johntd>
    <td>Doetd>
  tr>
  <tr>
    <td>Janetd>
    <td>Doetd>
  tr>
table>

<p>Table with thead, tfoot, and tbodyp>
<table>
  <thead>
    <tr>
      <th>Header content 1th>
      <th>Header content 2th>
    tr>
  thead>
  <tbody>
    <tr>
      <td>Body content 1td>
      <td>Body content 2td>
    tr>
  tbody>
  <tfoot>
    <tr>
      <td>Footer content 1td>
      <td>Footer content 2td>
    tr>
  tfoot>
table>

<p>Table with colgroupp>
<table>
  <colgroup span="4">colgroup>
  <tr>
    <th>Countriesth>
    <th>Capitalsth>
    <th>Populationth>
    <th>Languageth>
  tr>
  <tr>
    <td>USAtd>
    <td>Washington, D.C.td>
    <td>309 milliontd>
    <td>Englishtd>
  tr>
  <tr>
    <td>Swedentd>
    <td>Stockholmtd>
    <td>9 milliontd>
    <td>Swedishtd>
  tr>
table>

<p>Table with colgroup and colp>
<table>
  <colgroup>
    <col style="background-color: #0f0" />
    <col span="2" />
  colgroup>
  <tr>
    <th>Limeth>
    <th>Lemonth>
    <th>Orangeth>
  tr>
  <tr>
    <td>Greentd>
    <td>Yellowtd>
    <td>Orangetd>
  tr>
table>

<p>Simple table with captionp>
<table>
  <caption>
    Awesome caption
  caption>
  <tr>
    <td>Awesome datatd>
  tr>
table>
6 thành
<p>Simple table with headerp>
<table>
  <tr>
    <th>First nameth>
    <th>Last nameth>
  tr>
  <tr>
    <td>Johntd>
    <td>Doetd>
  tr>
  <tr>
    <td>Janetd>
    <td>Doetd>
  tr>
table>

<p>Table with thead, tfoot, and tbodyp>
<table>
  <thead>
    <tr>
      <th>Header content 1th>
      <th>Header content 2th>
    tr>
  thead>
  <tbody>
    <tr>
      <td>Body content 1td>
      <td>Body content 2td>
    tr>
  tbody>
  <tfoot>
    <tr>
      <td>Footer content 1td>
      <td>Footer content 2td>
    tr>
  tfoot>
table>

<p>Table with colgroupp>
<table>
  <colgroup span="4">colgroup>
  <tr>
    <th>Countriesth>
    <th>Capitalsth>
    <th>Populationth>
    <th>Languageth>
  tr>
  <tr>
    <td>USAtd>
    <td>Washington, D.C.td>
    <td>309 milliontd>
    <td>Englishtd>
  tr>
  <tr>
    <td>Swedentd>
    <td>Stockholmtd>
    <td>9 milliontd>
    <td>Swedishtd>
  tr>
table>

<p>Table with colgroup and colp>
<table>
  <colgroup>
    <col style="background-color: #0f0" />
    <col span="2" />
  colgroup>
  <tr>
    <th>Limeth>
    <th>Lemonth>
    <th>Orangeth>
  tr>
  <tr>
    <td>Greentd>
    <td>Yellowtd>
    <td>Orangetd>
  tr>
table>

<p>Simple table with captionp>
<table>
  <caption>
    Awesome caption
  caption>
  <tr>
    <td>Awesome datatd>
  tr>
table>
7 hoặc
<p>Simple table with headerp>
<table>
  <tr>
    <th>First nameth>
    <th>Last nameth>
  tr>
  <tr>
    <td>Johntd>
    <td>Doetd>
  tr>
  <tr>
    <td>Janetd>
    <td>Doetd>
  tr>
table>

<p>Table with thead, tfoot, and tbodyp>
<table>
  <thead>
    <tr>
      <th>Header content 1th>
      <th>Header content 2th>
    tr>
  thead>
  <tbody>
    <tr>
      <td>Body content 1td>
      <td>Body content 2td>
    tr>
  tbody>
  <tfoot>
    <tr>
      <td>Footer content 1td>
      <td>Footer content 2td>
    tr>
  tfoot>
table>

<p>Table with colgroupp>
<table>
  <colgroup span="4">colgroup>
  <tr>
    <th>Countriesth>
    <th>Capitalsth>
    <th>Populationth>
    <th>Languageth>
  tr>
  <tr>
    <td>USAtd>
    <td>Washington, D.C.td>
    <td>309 milliontd>
    <td>Englishtd>
  tr>
  <tr>
    <td>Swedentd>
    <td>Stockholmtd>
    <td>9 milliontd>
    <td>Swedishtd>
  tr>
table>

<p>Table with colgroup and colp>
<table>
  <colgroup>
    <col style="background-color: #0f0" />
    <col span="2" />
  colgroup>
  <tr>
    <th>Limeth>
    <th>Lemonth>
    <th>Orangeth>
  tr>
  <tr>
    <td>Greentd>
    <td>Yellowtd>
    <td>Orangetd>
  tr>
table>

<p>Simple table with captionp>
<table>
  <caption>
    Awesome caption
  caption>
  <tr>
    <td>Awesome datatd>
  tr>
table>
8 thành
<p>Simple table with headerp>
<table>
  <tr>
    <th>First nameth>
    <th>Last nameth>
  tr>
  <tr>
    <td>Johntd>
    <td>Doetd>
  tr>
  <tr>
    <td>Janetd>
    <td>Doetd>
  tr>
table>

<p>Table with thead, tfoot, and tbodyp>
<table>
  <thead>
    <tr>
      <th>Header content 1th>
      <th>Header content 2th>
    tr>
  thead>
  <tbody>
    <tr>
      <td>Body content 1td>
      <td>Body content 2td>
    tr>
  tbody>
  <tfoot>
    <tr>
      <td>Footer content 1td>
      <td>Footer content 2td>
    tr>
  tfoot>
table>

<p>Table with colgroupp>
<table>
  <colgroup span="4">colgroup>
  <tr>
    <th>Countriesth>
    <th>Capitalsth>
    <th>Populationth>
    <th>Languageth>
  tr>
  <tr>
    <td>USAtd>
    <td>Washington, D.C.td>
    <td>309 milliontd>
    <td>Englishtd>
  tr>
  <tr>
    <td>Swedentd>
    <td>Stockholmtd>
    <td>9 milliontd>
    <td>Swedishtd>
  tr>
table>

<p>Table with colgroup and colp>
<table>
  <colgroup>
    <col style="background-color: #0f0" />
    <col span="2" />
  colgroup>
  <tr>
    <th>Limeth>
    <th>Lemonth>
    <th>Orangeth>
  tr>
  <tr>
    <td>Greentd>
    <td>Yellowtd>
    <td>Orangetd>
  tr>
table>

<p>Simple table with captionp>
<table>
  <caption>
    Awesome caption
  caption>
  <tr>
    <td>Awesome datatd>
  tr>
table>
9 để đạt được hiệu ứng tương tự như thuộc tính căn chỉnh.

table {
  border-collapse: collapse;
  border-spacing: 0px;
}
table,
th,
td {
  padding: 5px;
  border: 1px solid black;
}
0Deprecated
Deprecated

Màu nền của bảng. Đây là mã RGB thập lục phân 6 chữ số, được đặt trước bởi một '

table {
  border-collapse: collapse;
  border-spacing: 0px;
}
table,
th,
td {
  padding: 5px;
  border: 1px solid black;
}
1'. Một trong những từ khóa màu được xác định trước cũng có thể được sử dụng.

Để đạt được hiệu ứng tương tự, hãy sử dụng thuộc tính CSS

table {
  border-collapse: collapse;
  border-spacing: 0px;
}
table,
th,
td {
  padding: 5px;
  border: 1px solid black;
}
2.

table {
  border-collapse: collapse;
  border-spacing: 0px;
}
table,
th,
td {
  padding: 5px;
  border: 1px solid black;
}
3Deprecated
Deprecated

Thuộc tính số nguyên này xác định, tính theo pixel, kích thước của khung xung quanh bảng. Nếu được đặt thành 0, thuộc tính

table {
  border-collapse: collapse;
  border-spacing: 0px;
}
table,
th,
td {
  padding: 5px;
  border: 1px solid black;
}
4 được đặt thành void.

Để đạt được hiệu ứng tương tự, hãy sử dụng thuộc tính tốc ký của CSS

table {
  border-collapse: collapse;
  border-spacing: 0px;
}
table,
th,
td {
  padding: 5px;
  border: 1px solid black;
}
3.

table {
  border-collapse: collapse;
  border-spacing: 0px;
}
table,
th,
td {
  padding: 5px;
  border: 1px solid black;
}
6Deprecated
Deprecated

Thuộc tính này xác định không gian giữa nội dung của một ô và đường viền của nó, được hiển thị hay không. Nếu chiều dài của cellpadding được xác định trong pixel, không gian có kích thước pixel này sẽ được áp dụng cho cả bốn mặt của nội dung của ô. Nếu độ dài được xác định bằng cách sử dụng giá trị phần trăm, nội dung sẽ được tập trung và tổng không gian dọc (trên cùng và dưới) sẽ biểu thị giá trị này. Điều tương tự cũng đúng với tổng không gian ngang (trái và phải).

Để đạt được hiệu ứng tương tự, hãy áp dụng thuộc tính

table {
  border-collapse: collapse;
  border-spacing: 0px;
}
table,
th,
td {
  padding: 5px;
  border: 1px solid black;
}
7 vào phần tử
<p>Simple table with headerp>
<table>
  <tr>
    <th>First nameth>
    <th>Last nameth>
  tr>
  <tr>
    <td>Johntd>
    <td>Doetd>
  tr>
  <tr>
    <td>Janetd>
    <td>Doetd>
  tr>
table>

<p>Table with thead, tfoot, and tbodyp>
<table>
  <thead>
    <tr>
      <th>Header content 1th>
      <th>Header content 2th>
    tr>
  thead>
  <tbody>
    <tr>
      <td>Body content 1td>
      <td>Body content 2td>
    tr>
  tbody>
  <tfoot>
    <tr>
      <td>Footer content 1td>
      <td>Footer content 2td>
    tr>
  tfoot>
table>

<p>Table with colgroupp>
<table>
  <colgroup span="4">colgroup>
  <tr>
    <th>Countriesth>
    <th>Capitalsth>
    <th>Populationth>
    <th>Languageth>
  tr>
  <tr>
    <td>USAtd>
    <td>Washington, D.C.td>
    <td>309 milliontd>
    <td>Englishtd>
  tr>
  <tr>
    <td>Swedentd>
    <td>Stockholmtd>
    <td>9 milliontd>
    <td>Swedishtd>
  tr>
table>

<p>Table with colgroup and colp>
<table>
  <colgroup>
    <col style="background-color: #0f0" />
    <col span="2" />
  colgroup>
  <tr>
    <th>Limeth>
    <th>Lemonth>
    <th>Orangeth>
  tr>
  <tr>
    <td>Greentd>
    <td>Yellowtd>
    <td>Orangetd>
  tr>
table>

<p>Simple table with captionp>
<table>
  <caption>
    Awesome caption
  caption>
  <tr>
    <td>Awesome datatd>
  tr>
table>
0, với giá trị của nó được đặt thành sụp đổ và thuộc tính
table {
  border-collapse: collapse;
  border-spacing: 0px;
}
table,
th,
td {
  padding: 5px;
  border: 1px solid black;
}
9 cho các phần tử
<table>
  <tbody>
    <tr>
      <td>3td>
    tr>
    <tr>
      <td>2td>
    tr>
    <tr>
      <td>1td>
    tr>
  tbody>
table>
0.

<table>
  <tbody>
    <tr>
      <td>3td>
    tr>
    <tr>
      <td>2td>
    tr>
    <tr>
      <td>1td>
    tr>
  tbody>
table>
1Deprecated
Deprecated

Thuộc tính này xác định kích thước của không gian giữa hai ô trong một giá trị phần trăm hoặc pixel. Thuộc tính được áp dụng cả theo chiều ngang và chiều dọc, đến khoảng cách giữa phần trên của bảng và các ô của hàng đầu tiên, bên trái của bảng và cột đầu tiên, bên phải của bảng và cột cuối cùng và đáy của Bảng và hàng cuối cùng.

Để đạt được hiệu ứng tương tự, hãy áp dụng thuộc tính

<table>
  <tbody>
    <tr>
      <td>3td>
    tr>
    <tr>
      <td>2td>
    tr>
    <tr>
      <td>1td>
    tr>
  tbody>
table>
2 vào phần tử
<p>Simple table with headerp>
<table>
  <tr>
    <th>First nameth>
    <th>Last nameth>
  tr>
  <tr>
    <td>Johntd>
    <td>Doetd>
  tr>
  <tr>
    <td>Janetd>
    <td>Doetd>
  tr>
table>

<p>Table with thead, tfoot, and tbodyp>
<table>
  <thead>
    <tr>
      <th>Header content 1th>
      <th>Header content 2th>
    tr>
  thead>
  <tbody>
    <tr>
      <td>Body content 1td>
      <td>Body content 2td>
    tr>
  tbody>
  <tfoot>
    <tr>
      <td>Footer content 1td>
      <td>Footer content 2td>
    tr>
  tfoot>
table>

<p>Table with colgroupp>
<table>
  <colgroup span="4">colgroup>
  <tr>
    <th>Countriesth>
    <th>Capitalsth>
    <th>Populationth>
    <th>Languageth>
  tr>
  <tr>
    <td>USAtd>
    <td>Washington, D.C.td>
    <td>309 milliontd>
    <td>Englishtd>
  tr>
  <tr>
    <td>Swedentd>
    <td>Stockholmtd>
    <td>9 milliontd>
    <td>Swedishtd>
  tr>
table>

<p>Table with colgroup and colp>
<table>
  <colgroup>
    <col style="background-color: #0f0" />
    <col span="2" />
  colgroup>
  <tr>
    <th>Limeth>
    <th>Lemonth>
    <th>Orangeth>
  tr>
  <tr>
    <td>Greentd>
    <td>Yellowtd>
    <td>Orangetd>
  tr>
table>

<p>Simple table with captionp>
<table>
  <caption>
    Awesome caption
  caption>
  <tr>
    <td>Awesome datatd>
  tr>
table>
0.
<table>
  <tbody>
    <tr>
      <td>3td>
    tr>
    <tr>
      <td>2td>
    tr>
    <tr>
      <td>1td>
    tr>
  tbody>
table>
2 không có bất kỳ ảnh hưởng nào nếu
table {
  border-collapse: collapse;
  border-spacing: 0px;
}
table,
th,
td {
  padding: 5px;
  border: 1px solid black;
}
7 được đặt thành sụp đổ.

table {
  border-collapse: collapse;
  border-spacing: 0px;
}
table,
th,
td {
  padding: 5px;
  border: 1px solid black;
}
4Deprecated
Deprecated

Thuộc tính được liệt kê này xác định phía nào của khung xung quanh bảng phải được hiển thị.

Để đạt được hiệu ứng tương tự, hãy sử dụng các thuộc tính

<table>
  <tbody>
    <tr>
      <td>3td>
    tr>
    <tr>
      <td>2td>
    tr>
    <tr>
      <td>1td>
    tr>
  tbody>
table>
7 và
<table>
  <tbody>
    <tr>
      <td>3td>
    tr>
    <tr>
      <td>2td>
    tr>
    <tr>
      <td>1td>
    tr>
  tbody>
table>
8.

<table>
  <tbody>
    <tr>
      <td>3td>
    tr>
    <tr>
      <td>2td>
    tr>
    <tr>
      <td>1td>
    tr>
  tbody>
table>
9Deprecated
Deprecated

Thuộc tính được liệt kê này xác định nơi các quy tắc, tức là các dòng, sẽ xuất hiện trong một bảng. Nó có thể có các giá trị sau:

  • HTMLTableSectionElement.prototype.sort = function (cb) {
      Array.from(this.rows)
        .sort(cb)
        .forEach((e) => this.appendChild(this.removeChild(e)));
    };
    
    document
      .querySelector("table")
      .tBodies[0].sort((a, b) => a.textContent.localeCompare(b.textContent));
    
    0, chỉ ra rằng sẽ không có quy tắc nào được hiển thị; Đó là giá trị mặc định;
  • HTMLTableSectionElement.prototype.sort = function (cb) {
      Array.from(this.rows)
        .sort(cb)
        .forEach((e) => this.appendChild(this.removeChild(e)));
    };
    
    document
      .querySelector("table")
      .tBodies[0].sort((a, b) => a.textContent.localeCompare(b.textContent));
    
    1, sẽ khiến các quy tắc được hiển thị giữa các nhóm hàng (được xác định bởi các phần tử
    HTMLTableSectionElement.prototype.sort = function (cb) {
      Array.from(this.rows)
        .sort(cb)
        .forEach((e) => this.appendChild(this.removeChild(e)));
    };
    
    document
      .querySelector("table")
      .tBodies[0].sort((a, b) => a.textContent.localeCompare(b.textContent));
    
    2,
    HTMLTableSectionElement.prototype.sort = function (cb) {
      Array.from(this.rows)
        .sort(cb)
        .forEach((e) => this.appendChild(this.removeChild(e)));
    };
    
    document
      .querySelector("table")
      .tBodies[0].sort((a, b) => a.textContent.localeCompare(b.textContent));
    
    3 và
    HTMLTableSectionElement.prototype.sort = function (cb) {
      Array.from(this.rows)
        .sort(cb)
        .forEach((e) => this.appendChild(this.removeChild(e)));
    };
    
    document
      .querySelector("table")
      .tBodies[0].sort((a, b) => a.textContent.localeCompare(b.textContent));
    
    4) và giữa các nhóm cột (được xác định bởi các phần tử
    HTMLTableSectionElement.prototype.sort = function (cb) {
      Array.from(this.rows)
        .sort(cb)
        .forEach((e) => this.appendChild(this.removeChild(e)));
    };
    
    document
      .querySelector("table")
      .tBodies[0].sort((a, b) => a.textContent.localeCompare(b.textContent));
    
    5 và
    HTMLTableSectionElement.prototype.sort = function (cb) {
      Array.from(this.rows)
        .sort(cb)
        .forEach((e) => this.appendChild(this.removeChild(e)));
    };
    
    document
      .querySelector("table")
      .tBodies[0].sort((a, b) => a.textContent.localeCompare(b.textContent));
    
    6);
  • HTMLTableSectionElement.prototype.sort = function (cb) {
      Array.from(this.rows)
        .sort(cb)
        .forEach((e) => this.appendChild(this.removeChild(e)));
    };
    
    document
      .querySelector("table")
      .tBodies[0].sort((a, b) => a.textContent.localeCompare(b.textContent));
    
    7, sẽ khiến các quy tắc được hiển thị giữa các hàng;
  • HTMLTableSectionElement.prototype.sort = function (cb) {
      Array.from(this.rows)
        .sort(cb)
        .forEach((e) => this.appendChild(this.removeChild(e)));
    };
    
    document
      .querySelector("table")
      .tBodies[0].sort((a, b) => a.textContent.localeCompare(b.textContent));
    
    8, sẽ khiến các quy tắc được hiển thị giữa các cột;
  • HTMLTableSectionElement.prototype.sort = function (cb) {
      Array.from(this.rows)
        .sort(cb)
        .forEach((e) => this.appendChild(this.removeChild(e)));
    };
    
    document
      .querySelector("table")
      .tBodies[0].sort((a, b) => a.textContent.localeCompare(b.textContent));
    
    9, sẽ khiến các quy tắc được hiển thị giữa các hàng và cột.

Để đạt được hiệu ứng tương tự, hãy áp dụng thuộc tính

table {
  border-collapse: collapse;
  border-spacing: 0px;
}
table,
th,
td {
  padding: 5px;
  border: 1px solid black;
}
3 vào các phần tử
HTMLTableSectionElement.prototype.sort = function (cb) {
  Array.from(this.rows)
    .sort(cb)
    .forEach((e) => this.appendChild(this.removeChild(e)));
};

document
  .querySelector("table")
  .tBodies[0].sort((a, b) => a.textContent.localeCompare(b.textContent));
2,
HTMLTableSectionElement.prototype.sort = function (cb) {
  Array.from(this.rows)
    .sort(cb)
    .forEach((e) => this.appendChild(this.removeChild(e)));
};

document
  .querySelector("table")
  .tBodies[0].sort((a, b) => a.textContent.localeCompare(b.textContent));
3,
HTMLTableSectionElement.prototype.sort = function (cb) {
  Array.from(this.rows)
    .sort(cb)
    .forEach((e) => this.appendChild(this.removeChild(e)));
};

document
  .querySelector("table")
  .tBodies[0].sort((a, b) => a.textContent.localeCompare(b.textContent));
4,
HTMLTableSectionElement.prototype.sort = function (cb) {
  Array.from(this.rows)
    .sort(cb)
    .forEach((e) => this.appendChild(this.removeChild(e)));
};

document
  .querySelector("table")
  .tBodies[0].sort((a, b) => a.textContent.localeCompare(b.textContent));
5 hoặc
HTMLTableSectionElement.prototype.sort = function (cb) {
  Array.from(this.rows)
    .sort(cb)
    .forEach((e) => this.appendChild(this.removeChild(e)));
};

document
  .querySelector("table")
  .tBodies[0].sort((a, b) => a.textContent.localeCompare(b.textContent));
6 thích hợp.

<table>
  <thead>
    <tr>
      <th>Numbersth>
      <th>Lettersth>
    tr>
  thead>
  <tbody>
    <tr>
      <td>3td>
      <td>Atd>
    tr>
    <tr>
      <td>2td>
      <td>Btd>
    tr>
    <tr>
      <td>1td>
      <td>Ctd>
    tr>
  tbody>
table>
6Deprecated
Deprecated

Thuộc tính này xác định một văn bản thay thế tóm tắt nội dung của bảng. Sử dụng phần tử

<table>
  <thead>
    <tr>
      <th>Numbersth>
      <th>Lettersth>
    tr>
  thead>
  <tbody>
    <tr>
      <td>3td>
      <td>Atd>
    tr>
    <tr>
      <td>2td>
      <td>Btd>
    tr>
    <tr>
      <td>1td>
      <td>Ctd>
    tr>
  tbody>
table>
7 thay thế.

<table>
  <thead>
    <tr>
      <th>Numbersth>
      <th>Lettersth>
    tr>
  thead>
  <tbody>
    <tr>
      <td>3td>
      <td>Atd>
    tr>
    <tr>
      <td>2td>
      <td>Btd>
    tr>
    <tr>
      <td>1td>
      <td>Ctd>
    tr>
  tbody>
table>
8Deprecated
Deprecated

Thuộc tính này xác định chiều rộng của bảng. Sử dụng thuộc tính CSS

<table>
  <thead>
    <tr>
      <th>Numbersth>
      <th>Lettersth>
    tr>
  thead>
  <tbody>
    <tr>
      <td>3td>
      <td>Atd>
    tr>
    <tr>
      <td>2td>
      <td>Btd>
    tr>
    <tr>
      <td>1td>
      <td>Ctd>
    tr>
  tbody>
table>
8 thay thế.

Ví dụ

Bảng đơn giản

<table>
  <tr>
    <td>Johntd>
    <td>Doetd>
  tr>
  <tr>
    <td>Janetd>
    <td>Doetd>
  tr>
table>

Ví dụ đơn giản hơn nữa

<p>Simple table with headerp>
<table>
  <tr>
    <th>First nameth>
    <th>Last nameth>
  tr>
  <tr>
    <td>Johntd>
    <td>Doetd>
  tr>
  <tr>
    <td>Janetd>
    <td>Doetd>
  tr>
table>

<p>Table with thead, tfoot, and tbodyp>
<table>
  <thead>
    <tr>
      <th>Header content 1th>
      <th>Header content 2th>
    tr>
  thead>
  <tbody>
    <tr>
      <td>Body content 1td>
      <td>Body content 2td>
    tr>
  tbody>
  <tfoot>
    <tr>
      <td>Footer content 1td>
      <td>Footer content 2td>
    tr>
  tfoot>
table>

<p>Table with colgroupp>
<table>
  <colgroup span="4">colgroup>
  <tr>
    <th>Countriesth>
    <th>Capitalsth>
    <th>Populationth>
    <th>Languageth>
  tr>
  <tr>
    <td>USAtd>
    <td>Washington, D.C.td>
    <td>309 milliontd>
    <td>Englishtd>
  tr>
  <tr>
    <td>Swedentd>
    <td>Stockholmtd>
    <td>9 milliontd>
    <td>Swedishtd>
  tr>
table>

<p>Table with colgroup and colp>
<table>
  <colgroup>
    <col style="background-color: #0f0" />
    <col span="2" />
  colgroup>
  <tr>
    <th>Limeth>
    <th>Lemonth>
    <th>Orangeth>
  tr>
  <tr>
    <td>Greentd>
    <td>Yellowtd>
    <td>Orangetd>
  tr>
table>

<p>Simple table with captionp>
<table>
  <caption>
    Awesome caption
  caption>
  <tr>
    <td>Awesome datatd>
  tr>
table>

table {
  border-collapse: collapse;
  border-spacing: 0px;
}
table,
th,
td {
  padding: 5px;
  border: 1px solid black;
}

Phân loại bảng

Sắp xếp các hàng bảng

Không có phương thức gốc để sắp xếp các hàng (các phần tử

const allTables = document.querySelectorAll("table");

for (const table of allTables) {
  const tBody = table.tBodies[0];
  const rows = Array.from(tBody.rows);
  const headerCells = table.tHead.rows[0].cells;

  for (const th of headerCells) {
    const cellIndex = th.cellIndex;

    th.addEventListener("click", () => {
      rows.sort((tr1, tr2) => {
        const tr1Text = tr1.cells[cellIndex].textContent;
        const tr2Text = tr2.cells[cellIndex].textContent;
        return tr1Text.localeCompare(tr2Text);
      });

      tBody.append(...rows);
    });
  }
}
0) của bảng HTML. Nhưng sử dụng
const allTables = document.querySelectorAll("table");

for (const table of allTables) {
  const tBody = table.tBodies[0];
  const rows = Array.from(tBody.rows);
  const headerCells = table.tHead.rows[0].cells;

  for (const th of headerCells) {
    const cellIndex = th.cellIndex;

    th.addEventListener("click", () => {
      rows.sort((tr1, tr2) => {
        const tr1Text = tr1.cells[cellIndex].textContent;
        const tr2Text = tr2.cells[cellIndex].textContent;
        return tr1Text.localeCompare(tr2Text);
      });

      tBody.append(...rows);
    });
  }
}
1,
const allTables = document.querySelectorAll("table");

for (const table of allTables) {
  const tBody = table.tBodies[0];
  const rows = Array.from(tBody.rows);
  const headerCells = table.tHead.rows[0].cells;

  for (const th of headerCells) {
    const cellIndex = th.cellIndex;

    th.addEventListener("click", () => {
      rows.sort((tr1, tr2) => {
        const tr1Text = tr1.cells[cellIndex].textContent;
        const tr2Text = tr2.cells[cellIndex].textContent;
        return tr1Text.localeCompare(tr2Text);
      });

      tBody.append(...rows);
    });
  }
}
2,
const allTables = document.querySelectorAll("table");

for (const table of allTables) {
  const tBody = table.tBodies[0];
  const rows = Array.from(tBody.rows);
  const headerCells = table.tHead.rows[0].cells;

  for (const th of headerCells) {
    const cellIndex = th.cellIndex;

    th.addEventListener("click", () => {
      rows.sort((tr1, tr2) => {
        const tr1Text = tr1.cells[cellIndex].textContent;
        const tr2Text = tr2.cells[cellIndex].textContent;
        return tr1Text.localeCompare(tr2Text);
      });

      tBody.append(...rows);
    });
  }
}
3 và
const allTables = document.querySelectorAll("table");

for (const table of allTables) {
  const tBody = table.tBodies[0];
  const rows = Array.from(tBody.rows);
  const headerCells = table.tHead.rows[0].cells;

  for (const th of headerCells) {
    const cellIndex = th.cellIndex;

    th.addEventListener("click", () => {
      rows.sort((tr1, tr2) => {
        const tr1Text = tr1.cells[cellIndex].textContent;
        const tr2Text = tr2.cells[cellIndex].textContent;
        return tr1Text.localeCompare(tr2Text);
      });

      tBody.append(...rows);
    });
  }
}
4, bạn có thể thực hiện chức năng
const allTables = document.querySelectorAll("table");

for (const table of allTables) {
  const tBody = table.tBodies[0];
  const rows = Array.from(tBody.rows);
  const headerCells = table.tHead.rows[0].cells;

  for (const th of headerCells) {
    const cellIndex = th.cellIndex;

    th.addEventListener("click", () => {
      rows.sort((tr1, tr2) => {
        const tr1Text = tr1.cells[cellIndex].textContent;
        const tr2Text = tr2.cells[cellIndex].textContent;
        return tr1Text.localeCompare(tr2Text);
      });

      tBody.append(...rows);
    });
  }
}
5 của riêng mình để sắp xếp
const allTables = document.querySelectorAll("table");

for (const table of allTables) {
  const tBody = table.tBodies[0];
  const rows = Array.from(tBody.rows);
  const headerCells = table.tHead.rows[0].cells;

  for (const th of headerCells) {
    const cellIndex = th.cellIndex;

    th.addEventListener("click", () => {
      rows.sort((tr1, tr2) => {
        const tr1Text = tr1.cells[cellIndex].textContent;
        const tr2Text = tr2.cells[cellIndex].textContent;
        return tr1Text.localeCompare(tr2Text);
      });

      tBody.append(...rows);
    });
  }
}
6 của các yếu tố
const allTables = document.querySelectorAll("table");

for (const table of allTables) {
  const tBody = table.tBodies[0];
  const rows = Array.from(tBody.rows);
  const headerCells = table.tHead.rows[0].cells;

  for (const th of headerCells) {
    const cellIndex = th.cellIndex;

    th.addEventListener("click", () => {
      rows.sort((tr1, tr2) => {
        const tr1Text = tr1.cells[cellIndex].textContent;
        const tr2Text = tr2.cells[cellIndex].textContent;
        return tr1Text.localeCompare(tr2Text);
      });

      tBody.append(...rows);
    });
  }
}
0.

Trong ví dụ dưới đây, bạn có thể thấy một ví dụ như vậy. Chúng tôi đang gắn nó vào phần tử để nó sắp xếp các ô bảng theo thứ tự tăng giá trị và cập nhật màn hình cho phù hợp.

HTML

<table>
  <tbody>
    <tr>
      <td>3td>
    tr>
    <tr>
      <td>2td>
    tr>
    <tr>
      <td>1td>
    tr>
  tbody>
table>

JavaScript

HTMLTableSectionElement.prototype.sort = function (cb) {
  Array.from(this.rows)
    .sort(cb)
    .forEach((e) => this.appendChild(this.removeChild(e)));
};

document
  .querySelector("table")
  .tBodies[0].sort((a, b) => a.textContent.localeCompare(b.textContent));

Kết quả

Sắp xếp các hàng bằng một cú nhấp chuột vào phần tử th

Ví dụ sau đây thêm một trình xử lý sự kiện vào mỗi yếu tố

const allTables = document.querySelectorAll("table");

for (const table of allTables) {
  const tBody = table.tBodies[0];
  const rows = Array.from(tBody.rows);
  const headerCells = table.tHead.rows[0].cells;

  for (const th of headerCells) {
    const cellIndex = th.cellIndex;

    th.addEventListener("click", () => {
      rows.sort((tr1, tr2) => {
        const tr1Text = tr1.cells[cellIndex].textContent;
        const tr2Text = tr2.cells[cellIndex].textContent;
        return tr1Text.localeCompare(tr2Text);
      });

      tBody.append(...rows);
    });
  }
}
8 của mỗi
<p>Simple table with headerp>
<table>
  <tr>
    <th>First nameth>
    <th>Last nameth>
  tr>
  <tr>
    <td>Johntd>
    <td>Doetd>
  tr>
  <tr>
    <td>Janetd>
    <td>Doetd>
  tr>
table>

<p>Table with thead, tfoot, and tbodyp>
<table>
  <thead>
    <tr>
      <th>Header content 1th>
      <th>Header content 2th>
    tr>
  thead>
  <tbody>
    <tr>
      <td>Body content 1td>
      <td>Body content 2td>
    tr>
  tbody>
  <tfoot>
    <tr>
      <td>Footer content 1td>
      <td>Footer content 2td>
    tr>
  tfoot>
table>

<p>Table with colgroupp>
<table>
  <colgroup span="4">colgroup>
  <tr>
    <th>Countriesth>
    <th>Capitalsth>
    <th>Populationth>
    <th>Languageth>
  tr>
  <tr>
    <td>USAtd>
    <td>Washington, D.C.td>
    <td>309 milliontd>
    <td>Englishtd>
  tr>
  <tr>
    <td>Swedentd>
    <td>Stockholmtd>
    <td>9 milliontd>
    <td>Swedishtd>
  tr>
table>

<p>Table with colgroup and colp>
<table>
  <colgroup>
    <col style="background-color: #0f0" />
    <col span="2" />
  colgroup>
  <tr>
    <th>Limeth>
    <th>Lemonth>
    <th>Orangeth>
  tr>
  <tr>
    <td>Greentd>
    <td>Yellowtd>
    <td>Orangetd>
  tr>
table>

<p>Simple table with captionp>
<table>
  <caption>
    Awesome caption
  caption>
  <tr>
    <td>Awesome datatd>
  tr>
table>
0 trong
<table>
  <thead>
    <tr>
      <th>1<sup>3sup> equals:
      <th>2<sup>3sup> equals:
      <th>3<sup>3sup> equals:
      <th>4<sup>3sup> equals:
      <th>5<sup>3sup> equals:
      <th>6<sup>3sup> equals:
      <th>7<sup>3sup> equals:
  <tbody>
    <tr>
      <td>row 1: 1
      <td>row 1: 8
      <td>row 1: 27
      <td>row 1: 64
      <td>row 1: 125
      <td>row 1: 216
      <td>row 1: 343
    <tr>
      <td>row 2: 1
      <td>row 2: 8
      <td>row 2: 27
      <td>row 2: 64
      <td>row 2: 125
      <td>row 2: 216
      <td>row 2: 343
    <tr>
      <td>row 3: 1
      <td>row 3: 8
      <td>row 3: 27
      <td>row 3: 64
      <td>row 3: 125
      <td>row 3: 216
      <td>row 3: 343
    <tr>
      <td>row 4: 1
      <td>row 4: 8
      <td>row 4: 27
      <td>row 4: 64
      <td>row 4: 125
      <td>row 4: 216
      <td>row 4: 343
    <tr>
      <td>row 5: 1
      <td>row 5: 8
      <td>row 5: 27
      <td>row 5: 64
      <td>row 5: 125
      <td>row 5: 216
      <td>row 5: 343
    <tr>
      <td>row 6: 1
      <td>row 6: 8
      <td>row 6: 27
      <td>row 6: 64
      <td>row 6: 125
      <td>row 6: 216
      <td>row 6: 343
    <tr>
      <td>row 7: 1
      <td>row 7: 8
      <td>row 7: 27
      <td>row 7: 64
      <td>row 7: 125
      <td>row 7: 216
      <td>row 7: 343
    <tr>
      <td>row 8: 1
      <td>row 8: 8
      <td>row 8: 27
      <td>row 8: 64
      <td>row 8: 125
      <td>row 8: 216
      <td>row 8: 343
    <tr>
      <td>row 9: 1
      <td>row 9: 8
      <td>row 9: 27
      <td>row 9: 64
      <td>row 9: 125
      <td>row 9: 216
      <td>row 9: 343
    <tr>
      <td>row 10: 1
      <td>row 10: 8
      <td>row 10: 27
      <td>row 10: 64
      <td>row 10: 125
      <td>row 10: 216
      <td>row 10: 343
    <tr>
      <td>row 11: 1
      <td>row 11: 8
      <td>row 11: 27
      <td>row 11: 64
      <td>row 11: 125
      <td>row 11: 216
      <td>row 11: 343
    <tr>
      <td>row 12: 1
      <td>row 12: 8
      <td>row 12: 27
      <td>row 12: 64
      <td>row 12: 125
      <td>row 12: 216
      <td>row 12: 343
    <tr>
      <td>row 13: 1
      <td>row 13: 8
      <td>row 13: 27
      <td>row 13: 64
      <td>row 13: 125
      <td>row 13: 216
      <td>row 13: 343
    <tr>
      <td>row 14: 1
      <td>row 14: 8
      <td>row 14: 27
      <td>row 14: 64
      <td>row 14: 125
      <td>row 14: 216
      <td>row 14: 343
    <tr>
      <td>row 15: 1
      <td>row 15: 8
      <td>row 15: 27
      <td>row 15: 64
      <td>row 15: 125
      <td>row 15: 216
      <td>row 15: 343
    <tr>
      <td>row 16: 1
      <td>row 16: 8
      <td>row 16: 27
      <td>row 16: 64
      <td>row 16: 125
      <td>row 16: 216
      <td>row 16: 343
    <tr>
      <td>row 17: 1
      <td>row 17: 8
      <td>row 17: 27
      <td>row 17: 64
      <td>row 17: 125
      <td>row 17: 216
      <td>row 17: 343
    <tr>
      <td>row 18: 1
      <td>row 18: 8
      <td>row 18: 27
      <td>row 18: 64
      <td>row 18: 125
      <td>row 18: 216
      <td>row 18: 343
    <tr>
      <td>row 19: 1
      <td>row 19: 8
      <td>row 19: 27
      <td>row 19: 64
      <td>row 19: 125
      <td>row 19: 216
      <td>row 19: 343
    <tr>
      <td>row 20: 1
      <td>row 20: 8
      <td>row 20: 27
      <td>row 20: 64
      <td>row 20: 125
      <td>row 20: 216
      <td>row 20: 343
table>
0; Nó sắp xếp tất cả các hàng của ____ 43, dựa trên việc sắp xếp trên các ô
<table>
  <thead>
    <tr>
      <th>1<sup>3sup> equals:
      <th>2<sup>3sup> equals:
      <th>3<sup>3sup> equals:
      <th>4<sup>3sup> equals:
      <th>5<sup>3sup> equals:
      <th>6<sup>3sup> equals:
      <th>7<sup>3sup> equals:
  <tbody>
    <tr>
      <td>row 1: 1
      <td>row 1: 8
      <td>row 1: 27
      <td>row 1: 64
      <td>row 1: 125
      <td>row 1: 216
      <td>row 1: 343
    <tr>
      <td>row 2: 1
      <td>row 2: 8
      <td>row 2: 27
      <td>row 2: 64
      <td>row 2: 125
      <td>row 2: 216
      <td>row 2: 343
    <tr>
      <td>row 3: 1
      <td>row 3: 8
      <td>row 3: 27
      <td>row 3: 64
      <td>row 3: 125
      <td>row 3: 216
      <td>row 3: 343
    <tr>
      <td>row 4: 1
      <td>row 4: 8
      <td>row 4: 27
      <td>row 4: 64
      <td>row 4: 125
      <td>row 4: 216
      <td>row 4: 343
    <tr>
      <td>row 5: 1
      <td>row 5: 8
      <td>row 5: 27
      <td>row 5: 64
      <td>row 5: 125
      <td>row 5: 216
      <td>row 5: 343
    <tr>
      <td>row 6: 1
      <td>row 6: 8
      <td>row 6: 27
      <td>row 6: 64
      <td>row 6: 125
      <td>row 6: 216
      <td>row 6: 343
    <tr>
      <td>row 7: 1
      <td>row 7: 8
      <td>row 7: 27
      <td>row 7: 64
      <td>row 7: 125
      <td>row 7: 216
      <td>row 7: 343
    <tr>
      <td>row 8: 1
      <td>row 8: 8
      <td>row 8: 27
      <td>row 8: 64
      <td>row 8: 125
      <td>row 8: 216
      <td>row 8: 343
    <tr>
      <td>row 9: 1
      <td>row 9: 8
      <td>row 9: 27
      <td>row 9: 64
      <td>row 9: 125
      <td>row 9: 216
      <td>row 9: 343
    <tr>
      <td>row 10: 1
      <td>row 10: 8
      <td>row 10: 27
      <td>row 10: 64
      <td>row 10: 125
      <td>row 10: 216
      <td>row 10: 343
    <tr>
      <td>row 11: 1
      <td>row 11: 8
      <td>row 11: 27
      <td>row 11: 64
      <td>row 11: 125
      <td>row 11: 216
      <td>row 11: 343
    <tr>
      <td>row 12: 1
      <td>row 12: 8
      <td>row 12: 27
      <td>row 12: 64
      <td>row 12: 125
      <td>row 12: 216
      <td>row 12: 343
    <tr>
      <td>row 13: 1
      <td>row 13: 8
      <td>row 13: 27
      <td>row 13: 64
      <td>row 13: 125
      <td>row 13: 216
      <td>row 13: 343
    <tr>
      <td>row 14: 1
      <td>row 14: 8
      <td>row 14: 27
      <td>row 14: 64
      <td>row 14: 125
      <td>row 14: 216
      <td>row 14: 343
    <tr>
      <td>row 15: 1
      <td>row 15: 8
      <td>row 15: 27
      <td>row 15: 64
      <td>row 15: 125
      <td>row 15: 216
      <td>row 15: 343
    <tr>
      <td>row 16: 1
      <td>row 16: 8
      <td>row 16: 27
      <td>row 16: 64
      <td>row 16: 125
      <td>row 16: 216
      <td>row 16: 343
    <tr>
      <td>row 17: 1
      <td>row 17: 8
      <td>row 17: 27
      <td>row 17: 64
      <td>row 17: 125
      <td>row 17: 216
      <td>row 17: 343
    <tr>
      <td>row 18: 1
      <td>row 18: 8
      <td>row 18: 27
      <td>row 18: 64
      <td>row 18: 125
      <td>row 18: 216
      <td>row 18: 343
    <tr>
      <td>row 19: 1
      <td>row 19: 8
      <td>row 19: 27
      <td>row 19: 64
      <td>row 19: 125
      <td>row 19: 216
      <td>row 19: 343
    <tr>
      <td>row 20: 1
      <td>row 20: 8
      <td>row 20: 27
      <td>row 20: 64
      <td>row 20: 125
      <td>row 20: 216
      <td>row 20: 343
table>
2 có trong các hàng.

Lưu ý: Giải pháp này giả định rằng các yếu tố

<table>
  <tbody>
    <tr>
      <td>3td>
    tr>
    <tr>
      <td>2td>
    tr>
    <tr>
      <td>1td>
    tr>
  tbody>
table>
0 được điền bởi văn bản thô không có phần tử hậu duệ. This solution assumes that the
<table>
  <tbody>
    <tr>
      <td>3td>
    tr>
    <tr>
      <td>2td>
    tr>
    <tr>
      <td>1td>
    tr>
  tbody>
table>
0 elements are populated by raw text with no descendant elements.

HTML

<table>
  <thead>
    <tr>
      <th>Numbersth>
      <th>Lettersth>
    tr>
  thead>
  <tbody>
    <tr>
      <td>3td>
      <td>Atd>
    tr>
    <tr>
      <td>2td>
      <td>Btd>
    tr>
    <tr>
      <td>1td>
      <td>Ctd>
    tr>
  tbody>
table>

JavaScript

const allTables = document.querySelectorAll("table");

for (const table of allTables) {
  const tBody = table.tBodies[0];
  const rows = Array.from(tBody.rows);
  const headerCells = table.tHead.rows[0].cells;

  for (const th of headerCells) {
    const cellIndex = th.cellIndex;

    th.addEventListener("click", () => {
      rows.sort((tr1, tr2) => {
        const tr1Text = tr1.cells[cellIndex].textContent;
        const tr2Text = tr2.cells[cellIndex].textContent;
        return tr1Text.localeCompare(tr2Text);
      });

      tBody.append(...rows);
    });
  }
}

Kết quả

Hiển thị các bảng lớn trong không gian nhỏ

Một vấn đề phổ biến với các bảng trên web là chúng không hoạt động rất tốt trên các màn hình nhỏ khi lượng nội dung lớn và cách làm cho chúng có thể cuộn được không rõ ràng, đặc biệt là khi đánh dấu có thể đến từ CDN và không thể được sửa đổi để có một trình bao bọc.

Ví dụ này cung cấp một cách để hiển thị các bảng trong không gian nhỏ. Chúng tôi đã ẩn nội dung HTML vì nó rất lớn và không có gì đáng chú ý về nó. CSS hữu ích hơn để kiểm tra trong ví dụ này.

<table>
  <thead>
    <tr>
      <th>1<sup>3sup> equals:
      <th>2<sup>3sup> equals:
      <th>3<sup>3sup> equals:
      <th>4<sup>3sup> equals:
      <th>5<sup>3sup> equals:
      <th>6<sup>3sup> equals:
      <th>7<sup>3sup> equals:
  <tbody>
    <tr>
      <td>row 1: 1
      <td>row 1: 8
      <td>row 1: 27
      <td>row 1: 64
      <td>row 1: 125
      <td>row 1: 216
      <td>row 1: 343
    <tr>
      <td>row 2: 1
      <td>row 2: 8
      <td>row 2: 27
      <td>row 2: 64
      <td>row 2: 125
      <td>row 2: 216
      <td>row 2: 343
    <tr>
      <td>row 3: 1
      <td>row 3: 8
      <td>row 3: 27
      <td>row 3: 64
      <td>row 3: 125
      <td>row 3: 216
      <td>row 3: 343
    <tr>
      <td>row 4: 1
      <td>row 4: 8
      <td>row 4: 27
      <td>row 4: 64
      <td>row 4: 125
      <td>row 4: 216
      <td>row 4: 343
    <tr>
      <td>row 5: 1
      <td>row 5: 8
      <td>row 5: 27
      <td>row 5: 64
      <td>row 5: 125
      <td>row 5: 216
      <td>row 5: 343
    <tr>
      <td>row 6: 1
      <td>row 6: 8
      <td>row 6: 27
      <td>row 6: 64
      <td>row 6: 125
      <td>row 6: 216
      <td>row 6: 343
    <tr>
      <td>row 7: 1
      <td>row 7: 8
      <td>row 7: 27
      <td>row 7: 64
      <td>row 7: 125
      <td>row 7: 216
      <td>row 7: 343
    <tr>
      <td>row 8: 1
      <td>row 8: 8
      <td>row 8: 27
      <td>row 8: 64
      <td>row 8: 125
      <td>row 8: 216
      <td>row 8: 343
    <tr>
      <td>row 9: 1
      <td>row 9: 8
      <td>row 9: 27
      <td>row 9: 64
      <td>row 9: 125
      <td>row 9: 216
      <td>row 9: 343
    <tr>
      <td>row 10: 1
      <td>row 10: 8
      <td>row 10: 27
      <td>row 10: 64
      <td>row 10: 125
      <td>row 10: 216
      <td>row 10: 343
    <tr>
      <td>row 11: 1
      <td>row 11: 8
      <td>row 11: 27
      <td>row 11: 64
      <td>row 11: 125
      <td>row 11: 216
      <td>row 11: 343
    <tr>
      <td>row 12: 1
      <td>row 12: 8
      <td>row 12: 27
      <td>row 12: 64
      <td>row 12: 125
      <td>row 12: 216
      <td>row 12: 343
    <tr>
      <td>row 13: 1
      <td>row 13: 8
      <td>row 13: 27
      <td>row 13: 64
      <td>row 13: 125
      <td>row 13: 216
      <td>row 13: 343
    <tr>
      <td>row 14: 1
      <td>row 14: 8
      <td>row 14: 27
      <td>row 14: 64
      <td>row 14: 125
      <td>row 14: 216
      <td>row 14: 343
    <tr>
      <td>row 15: 1
      <td>row 15: 8
      <td>row 15: 27
      <td>row 15: 64
      <td>row 15: 125
      <td>row 15: 216
      <td>row 15: 343
    <tr>
      <td>row 16: 1
      <td>row 16: 8
      <td>row 16: 27
      <td>row 16: 64
      <td>row 16: 125
      <td>row 16: 216
      <td>row 16: 343
    <tr>
      <td>row 17: 1
      <td>row 17: 8
      <td>row 17: 27
      <td>row 17: 64
      <td>row 17: 125
      <td>row 17: 216
      <td>row 17: 343
    <tr>
      <td>row 18: 1
      <td>row 18: 8
      <td>row 18: 27
      <td>row 18: 64
      <td>row 18: 125
      <td>row 18: 216
      <td>row 18: 343
    <tr>
      <td>row 19: 1
      <td>row 19: 8
      <td>row 19: 27
      <td>row 19: 64
      <td>row 19: 125
      <td>row 19: 216
      <td>row 19: 343
    <tr>
      <td>row 20: 1
      <td>row 20: 8
      <td>row 20: 27
      <td>row 20: 64
      <td>row 20: 125
      <td>row 20: 216
      <td>row 20: 343
table>

Khi nhìn vào các kiểu này, bạn sẽ nhận thấy rằng thuộc tính ____74 của bảng đã được đặt thành

<table>
  <thead>
    <tr>
      <th>1<sup>3sup> equals:
      <th>2<sup>3sup> equals:
      <th>3<sup>3sup> equals:
      <th>4<sup>3sup> equals:
      <th>5<sup>3sup> equals:
      <th>6<sup>3sup> equals:
      <th>7<sup>3sup> equals:
  <tbody>
    <tr>
      <td>row 1: 1
      <td>row 1: 8
      <td>row 1: 27
      <td>row 1: 64
      <td>row 1: 125
      <td>row 1: 216
      <td>row 1: 343
    <tr>
      <td>row 2: 1
      <td>row 2: 8
      <td>row 2: 27
      <td>row 2: 64
      <td>row 2: 125
      <td>row 2: 216
      <td>row 2: 343
    <tr>
      <td>row 3: 1
      <td>row 3: 8
      <td>row 3: 27
      <td>row 3: 64
      <td>row 3: 125
      <td>row 3: 216
      <td>row 3: 343
    <tr>
      <td>row 4: 1
      <td>row 4: 8
      <td>row 4: 27
      <td>row 4: 64
      <td>row 4: 125
      <td>row 4: 216
      <td>row 4: 343
    <tr>
      <td>row 5: 1
      <td>row 5: 8
      <td>row 5: 27
      <td>row 5: 64
      <td>row 5: 125
      <td>row 5: 216
      <td>row 5: 343
    <tr>
      <td>row 6: 1
      <td>row 6: 8
      <td>row 6: 27
      <td>row 6: 64
      <td>row 6: 125
      <td>row 6: 216
      <td>row 6: 343
    <tr>
      <td>row 7: 1
      <td>row 7: 8
      <td>row 7: 27
      <td>row 7: 64
      <td>row 7: 125
      <td>row 7: 216
      <td>row 7: 343
    <tr>
      <td>row 8: 1
      <td>row 8: 8
      <td>row 8: 27
      <td>row 8: 64
      <td>row 8: 125
      <td>row 8: 216
      <td>row 8: 343
    <tr>
      <td>row 9: 1
      <td>row 9: 8
      <td>row 9: 27
      <td>row 9: 64
      <td>row 9: 125
      <td>row 9: 216
      <td>row 9: 343
    <tr>
      <td>row 10: 1
      <td>row 10: 8
      <td>row 10: 27
      <td>row 10: 64
      <td>row 10: 125
      <td>row 10: 216
      <td>row 10: 343
    <tr>
      <td>row 11: 1
      <td>row 11: 8
      <td>row 11: 27
      <td>row 11: 64
      <td>row 11: 125
      <td>row 11: 216
      <td>row 11: 343
    <tr>
      <td>row 12: 1
      <td>row 12: 8
      <td>row 12: 27
      <td>row 12: 64
      <td>row 12: 125
      <td>row 12: 216
      <td>row 12: 343
    <tr>
      <td>row 13: 1
      <td>row 13: 8
      <td>row 13: 27
      <td>row 13: 64
      <td>row 13: 125
      <td>row 13: 216
      <td>row 13: 343
    <tr>
      <td>row 14: 1
      <td>row 14: 8
      <td>row 14: 27
      <td>row 14: 64
      <td>row 14: 125
      <td>row 14: 216
      <td>row 14: 343
    <tr>
      <td>row 15: 1
      <td>row 15: 8
      <td>row 15: 27
      <td>row 15: 64
      <td>row 15: 125
      <td>row 15: 216
      <td>row 15: 343
    <tr>
      <td>row 16: 1
      <td>row 16: 8
      <td>row 16: 27
      <td>row 16: 64
      <td>row 16: 125
      <td>row 16: 216
      <td>row 16: 343
    <tr>
      <td>row 17: 1
      <td>row 17: 8
      <td>row 17: 27
      <td>row 17: 64
      <td>row 17: 125
      <td>row 17: 216
      <td>row 17: 343
    <tr>
      <td>row 18: 1
      <td>row 18: 8
      <td>row 18: 27
      <td>row 18: 64
      <td>row 18: 125
      <td>row 18: 216
      <td>row 18: 343
    <tr>
      <td>row 19: 1
      <td>row 19: 8
      <td>row 19: 27
      <td>row 19: 64
      <td>row 19: 125
      <td>row 19: 216
      <td>row 19: 343
    <tr>
      <td>row 20: 1
      <td>row 20: 8
      <td>row 20: 27
      <td>row 20: 64
      <td>row 20: 125
      <td>row 20: 216
      <td>row 20: 343
table>
5. Mặc dù điều này cho phép cuộn, bảng mất một số tính toàn vẹn của nó và các ô bảng cố gắng trở nên nhỏ nhất có thể. Để giảm thiểu vấn đề này, chúng tôi đã đặt
<table>
  <thead>
    <tr>
      <th>1<sup>3sup> equals:
      <th>2<sup>3sup> equals:
      <th>3<sup>3sup> equals:
      <th>4<sup>3sup> equals:
      <th>5<sup>3sup> equals:
      <th>6<sup>3sup> equals:
      <th>7<sup>3sup> equals:
  <tbody>
    <tr>
      <td>row 1: 1
      <td>row 1: 8
      <td>row 1: 27
      <td>row 1: 64
      <td>row 1: 125
      <td>row 1: 216
      <td>row 1: 343
    <tr>
      <td>row 2: 1
      <td>row 2: 8
      <td>row 2: 27
      <td>row 2: 64
      <td>row 2: 125
      <td>row 2: 216
      <td>row 2: 343
    <tr>
      <td>row 3: 1
      <td>row 3: 8
      <td>row 3: 27
      <td>row 3: 64
      <td>row 3: 125
      <td>row 3: 216
      <td>row 3: 343
    <tr>
      <td>row 4: 1
      <td>row 4: 8
      <td>row 4: 27
      <td>row 4: 64
      <td>row 4: 125
      <td>row 4: 216
      <td>row 4: 343
    <tr>
      <td>row 5: 1
      <td>row 5: 8
      <td>row 5: 27
      <td>row 5: 64
      <td>row 5: 125
      <td>row 5: 216
      <td>row 5: 343
    <tr>
      <td>row 6: 1
      <td>row 6: 8
      <td>row 6: 27
      <td>row 6: 64
      <td>row 6: 125
      <td>row 6: 216
      <td>row 6: 343
    <tr>
      <td>row 7: 1
      <td>row 7: 8
      <td>row 7: 27
      <td>row 7: 64
      <td>row 7: 125
      <td>row 7: 216
      <td>row 7: 343
    <tr>
      <td>row 8: 1
      <td>row 8: 8
      <td>row 8: 27
      <td>row 8: 64
      <td>row 8: 125
      <td>row 8: 216
      <td>row 8: 343
    <tr>
      <td>row 9: 1
      <td>row 9: 8
      <td>row 9: 27
      <td>row 9: 64
      <td>row 9: 125
      <td>row 9: 216
      <td>row 9: 343
    <tr>
      <td>row 10: 1
      <td>row 10: 8
      <td>row 10: 27
      <td>row 10: 64
      <td>row 10: 125
      <td>row 10: 216
      <td>row 10: 343
    <tr>
      <td>row 11: 1
      <td>row 11: 8
      <td>row 11: 27
      <td>row 11: 64
      <td>row 11: 125
      <td>row 11: 216
      <td>row 11: 343
    <tr>
      <td>row 12: 1
      <td>row 12: 8
      <td>row 12: 27
      <td>row 12: 64
      <td>row 12: 125
      <td>row 12: 216
      <td>row 12: 343
    <tr>
      <td>row 13: 1
      <td>row 13: 8
      <td>row 13: 27
      <td>row 13: 64
      <td>row 13: 125
      <td>row 13: 216
      <td>row 13: 343
    <tr>
      <td>row 14: 1
      <td>row 14: 8
      <td>row 14: 27
      <td>row 14: 64
      <td>row 14: 125
      <td>row 14: 216
      <td>row 14: 343
    <tr>
      <td>row 15: 1
      <td>row 15: 8
      <td>row 15: 27
      <td>row 15: 64
      <td>row 15: 125
      <td>row 15: 216
      <td>row 15: 343
    <tr>
      <td>row 16: 1
      <td>row 16: 8
      <td>row 16: 27
      <td>row 16: 64
      <td>row 16: 125
      <td>row 16: 216
      <td>row 16: 343
    <tr>
      <td>row 17: 1
      <td>row 17: 8
      <td>row 17: 27
      <td>row 17: 64
      <td>row 17: 125
      <td>row 17: 216
      <td>row 17: 343
    <tr>
      <td>row 18: 1
      <td>row 18: 8
      <td>row 18: 27
      <td>row 18: 64
      <td>row 18: 125
      <td>row 18: 216
      <td>row 18: 343
    <tr>
      <td>row 19: 1
      <td>row 19: 8
      <td>row 19: 27
      <td>row 19: 64
      <td>row 19: 125
      <td>row 19: 216
      <td>row 19: 343
    <tr>
      <td>row 20: 1
      <td>row 20: 8
      <td>row 20: 27
      <td>row 20: 64
      <td>row 20: 125
      <td>row 20: 216
      <td>row 20: 343
table>
6 thành
<table>
  <thead>
    <tr>
      <th>1<sup>3sup> equals:
      <th>2<sup>3sup> equals:
      <th>3<sup>3sup> equals:
      <th>4<sup>3sup> equals:
      <th>5<sup>3sup> equals:
      <th>6<sup>3sup> equals:
      <th>7<sup>3sup> equals:
  <tbody>
    <tr>
      <td>row 1: 1
      <td>row 1: 8
      <td>row 1: 27
      <td>row 1: 64
      <td>row 1: 125
      <td>row 1: 216
      <td>row 1: 343
    <tr>
      <td>row 2: 1
      <td>row 2: 8
      <td>row 2: 27
      <td>row 2: 64
      <td>row 2: 125
      <td>row 2: 216
      <td>row 2: 343
    <tr>
      <td>row 3: 1
      <td>row 3: 8
      <td>row 3: 27
      <td>row 3: 64
      <td>row 3: 125
      <td>row 3: 216
      <td>row 3: 343
    <tr>
      <td>row 4: 1
      <td>row 4: 8
      <td>row 4: 27
      <td>row 4: 64
      <td>row 4: 125
      <td>row 4: 216
      <td>row 4: 343
    <tr>
      <td>row 5: 1
      <td>row 5: 8
      <td>row 5: 27
      <td>row 5: 64
      <td>row 5: 125
      <td>row 5: 216
      <td>row 5: 343
    <tr>
      <td>row 6: 1
      <td>row 6: 8
      <td>row 6: 27
      <td>row 6: 64
      <td>row 6: 125
      <td>row 6: 216
      <td>row 6: 343
    <tr>
      <td>row 7: 1
      <td>row 7: 8
      <td>row 7: 27
      <td>row 7: 64
      <td>row 7: 125
      <td>row 7: 216
      <td>row 7: 343
    <tr>
      <td>row 8: 1
      <td>row 8: 8
      <td>row 8: 27
      <td>row 8: 64
      <td>row 8: 125
      <td>row 8: 216
      <td>row 8: 343
    <tr>
      <td>row 9: 1
      <td>row 9: 8
      <td>row 9: 27
      <td>row 9: 64
      <td>row 9: 125
      <td>row 9: 216
      <td>row 9: 343
    <tr>
      <td>row 10: 1
      <td>row 10: 8
      <td>row 10: 27
      <td>row 10: 64
      <td>row 10: 125
      <td>row 10: 216
      <td>row 10: 343
    <tr>
      <td>row 11: 1
      <td>row 11: 8
      <td>row 11: 27
      <td>row 11: 64
      <td>row 11: 125
      <td>row 11: 216
      <td>row 11: 343
    <tr>
      <td>row 12: 1
      <td>row 12: 8
      <td>row 12: 27
      <td>row 12: 64
      <td>row 12: 125
      <td>row 12: 216
      <td>row 12: 343
    <tr>
      <td>row 13: 1
      <td>row 13: 8
      <td>row 13: 27
      <td>row 13: 64
      <td>row 13: 125
      <td>row 13: 216
      <td>row 13: 343
    <tr>
      <td>row 14: 1
      <td>row 14: 8
      <td>row 14: 27
      <td>row 14: 64
      <td>row 14: 125
      <td>row 14: 216
      <td>row 14: 343
    <tr>
      <td>row 15: 1
      <td>row 15: 8
      <td>row 15: 27
      <td>row 15: 64
      <td>row 15: 125
      <td>row 15: 216
      <td>row 15: 343
    <tr>
      <td>row 16: 1
      <td>row 16: 8
      <td>row 16: 27
      <td>row 16: 64
      <td>row 16: 125
      <td>row 16: 216
      <td>row 16: 343
    <tr>
      <td>row 17: 1
      <td>row 17: 8
      <td>row 17: 27
      <td>row 17: 64
      <td>row 17: 125
      <td>row 17: 216
      <td>row 17: 343
    <tr>
      <td>row 18: 1
      <td>row 18: 8
      <td>row 18: 27
      <td>row 18: 64
      <td>row 18: 125
      <td>row 18: 216
      <td>row 18: 343
    <tr>
      <td>row 19: 1
      <td>row 19: 8
      <td>row 19: 27
      <td>row 19: 64
      <td>row 19: 125
      <td>row 19: 216
      <td>row 19: 343
    <tr>
      <td>row 20: 1
      <td>row 20: 8
      <td>row 20: 27
      <td>row 20: 64
      <td>row 20: 125
      <td>row 20: 216
      <td>row 20: 343
table>
7 trên
HTMLTableSectionElement.prototype.sort = function (cb) {
  Array.from(this.rows)
    .sort(cb)
    .forEach((e) => this.appendChild(this.removeChild(e)));
};

document
  .querySelector("table")
  .tBodies[0].sort((a, b) => a.textContent.localeCompare(b.textContent));
3. Tuy nhiên, chúng tôi không làm điều này cho
HTMLTableSectionElement.prototype.sort = function (cb) {
  Array.from(this.rows)
    .sort(cb)
    .forEach((e) => this.appendChild(this.removeChild(e)));
};

document
  .querySelector("table")
  .tBodies[0].sort((a, b) => a.textContent.localeCompare(b.textContent));
2 để tránh các tiêu đề dài buộc các cột phải rộng hơn chúng cần phải hiển thị dữ liệu.

Để giữ các tiêu đề bảng trên trang trong khi cuộn xuống, chúng tôi đã đặt

table,
th,
td {
  border: 1px solid;
}

table {
  width: 100%;
  max-width: 400px;
  height: 240px;
  margin: 0 auto;
  display: block;
  overflow-x: auto;
  border-spacing: 0;
}

tbody {
  white-space: nowrap;
}

th,
td {
  padding: 5px 10px;
  border-top-width: 0;
  border-left-width: 0;
}

th {
  position: sticky;
  top: 0;
  background: #fff;
  vertical-align: bottom;
}

th:last-child,
td:last-child {
  border-right-width: 0;
}

tr:last-child td {
  border-bottom-width: 0;
}
0 thành các yếu tố
const allTables = document.querySelectorAll("table");

for (const table of allTables) {
  const tBody = table.tBodies[0];
  const rows = Array.from(tBody.rows);
  const headerCells = table.tHead.rows[0].cells;

  for (const th of headerCells) {
    const cellIndex = th.cellIndex;

    th.addEventListener("click", () => {
      rows.sort((tr1, tr2) => {
        const tr1Text = tr1.cells[cellIndex].textContent;
        const tr2Text = tr2.cells[cellIndex].textContent;
        return tr1Text.localeCompare(tr2Text);
      });

      tBody.append(...rows);
    });
  }
}
8. Lưu ý rằng chúng tôi chưa đặt
table {
  border-collapse: collapse;
  border-spacing: 0px;
}
table,
th,
td {
  padding: 5px;
  border: 1px solid black;
}
7 thành
table,
th,
td {
  border: 1px solid;
}

table {
  width: 100%;
  max-width: 400px;
  height: 240px;
  margin: 0 auto;
  display: block;
  overflow-x: auto;
  border-spacing: 0;
}

tbody {
  white-space: nowrap;
}

th,
td {
  padding: 5px 10px;
  border-top-width: 0;
  border-left-width: 0;
}

th {
  position: sticky;
  top: 0;
  background: #fff;
  vertical-align: bottom;
}

th:last-child,
td:last-child {
  border-right-width: 0;
}

tr:last-child td {
  border-bottom-width: 0;
}
3, vì chúng tôi thực hiện tiêu đề không thể tách chính xác khỏi phần còn lại của bảng.not set
table {
  border-collapse: collapse;
  border-spacing: 0px;
}
table,
th,
td {
  padding: 5px;
  border: 1px solid black;
}
7 to
table,
th,
td {
  border: 1px solid;
}

table {
  width: 100%;
  max-width: 400px;
  height: 240px;
  margin: 0 auto;
  display: block;
  overflow-x: auto;
  border-spacing: 0;
}

tbody {
  white-space: nowrap;
}

th,
td {
  padding: 5px 10px;
  border-top-width: 0;
  border-left-width: 0;
}

th {
  position: sticky;
  top: 0;
  background: #fff;
  vertical-align: bottom;
}

th:last-child,
td:last-child {
  border-right-width: 0;
}

tr:last-child td {
  border-bottom-width: 0;
}
3, as if we do the header cannot be separated correctly from the rest of the table.

table,
th,
td {
  border: 1px solid;
}

table {
  width: 100%;
  max-width: 400px;
  height: 240px;
  margin: 0 auto;
  display: block;
  overflow-x: auto;
  border-spacing: 0;
}

tbody {
  white-space: nowrap;
}

th,
td {
  padding: 5px 10px;
  border-top-width: 0;
  border-left-width: 0;
}

th {
  position: sticky;
  top: 0;
  background: #fff;
  vertical-align: bottom;
}

th:last-child,
td:last-child {
  border-right-width: 0;
}

tr:last-child td {
  border-bottom-width: 0;
}

Kết quả

Hiển thị các bảng lớn trong không gian nhỏ

Một vấn đề phổ biến với các bảng trên web là chúng không hoạt động rất tốt trên các màn hình nhỏ khi lượng nội dung lớn và cách làm cho chúng có thể cuộn được không rõ ràng, đặc biệt là khi đánh dấu có thể đến từ CDN và không thể được sửa đổi để có một trình bao bọc.

Ví dụ này cung cấp một cách để hiển thị các bảng trong không gian nhỏ. Chúng tôi đã ẩn nội dung HTML vì nó rất lớn và không có gì đáng chú ý về nó. CSS hữu ích hơn để kiểm tra trong ví dụ này.

Khi nhìn vào các kiểu này, bạn sẽ nhận thấy rằng thuộc tính ____74 của bảng đã được đặt thành

<table>
  <thead>
    <tr>
      <th>1<sup>3sup> equals:
      <th>2<sup>3sup> equals:
      <th>3<sup>3sup> equals:
      <th>4<sup>3sup> equals:
      <th>5<sup>3sup> equals:
      <th>6<sup>3sup> equals:
      <th>7<sup>3sup> equals:
  <tbody>
    <tr>
      <td>row 1: 1
      <td>row 1: 8
      <td>row 1: 27
      <td>row 1: 64
      <td>row 1: 125
      <td>row 1: 216
      <td>row 1: 343
    <tr>
      <td>row 2: 1
      <td>row 2: 8
      <td>row 2: 27
      <td>row 2: 64
      <td>row 2: 125
      <td>row 2: 216
      <td>row 2: 343
    <tr>
      <td>row 3: 1
      <td>row 3: 8
      <td>row 3: 27
      <td>row 3: 64
      <td>row 3: 125
      <td>row 3: 216
      <td>row 3: 343
    <tr>
      <td>row 4: 1
      <td>row 4: 8
      <td>row 4: 27
      <td>row 4: 64
      <td>row 4: 125
      <td>row 4: 216
      <td>row 4: 343
    <tr>
      <td>row 5: 1
      <td>row 5: 8
      <td>row 5: 27
      <td>row 5: 64
      <td>row 5: 125
      <td>row 5: 216
      <td>row 5: 343
    <tr>
      <td>row 6: 1
      <td>row 6: 8
      <td>row 6: 27
      <td>row 6: 64
      <td>row 6: 125
      <td>row 6: 216
      <td>row 6: 343
    <tr>
      <td>row 7: 1
      <td>row 7: 8
      <td>row 7: 27
      <td>row 7: 64
      <td>row 7: 125
      <td>row 7: 216
      <td>row 7: 343
    <tr>
      <td>row 8: 1
      <td>row 8: 8
      <td>row 8: 27
      <td>row 8: 64
      <td>row 8: 125
      <td>row 8: 216
      <td>row 8: 343
    <tr>
      <td>row 9: 1
      <td>row 9: 8
      <td>row 9: 27
      <td>row 9: 64
      <td>row 9: 125
      <td>row 9: 216
      <td>row 9: 343
    <tr>
      <td>row 10: 1
      <td>row 10: 8
      <td>row 10: 27
      <td>row 10: 64
      <td>row 10: 125
      <td>row 10: 216
      <td>row 10: 343
    <tr>
      <td>row 11: 1
      <td>row 11: 8
      <td>row 11: 27
      <td>row 11: 64
      <td>row 11: 125
      <td>row 11: 216
      <td>row 11: 343
    <tr>
      <td>row 12: 1
      <td>row 12: 8
      <td>row 12: 27
      <td>row 12: 64
      <td>row 12: 125
      <td>row 12: 216
      <td>row 12: 343
    <tr>
      <td>row 13: 1
      <td>row 13: 8
      <td>row 13: 27
      <td>row 13: 64
      <td>row 13: 125
      <td>row 13: 216
      <td>row 13: 343
    <tr>
      <td>row 14: 1
      <td>row 14: 8
      <td>row 14: 27
      <td>row 14: 64
      <td>row 14: 125
      <td>row 14: 216
      <td>row 14: 343
    <tr>
      <td>row 15: 1
      <td>row 15: 8
      <td>row 15: 27
      <td>row 15: 64
      <td>row 15: 125
      <td>row 15: 216
      <td>row 15: 343
    <tr>
      <td>row 16: 1
      <td>row 16: 8
      <td>row 16: 27
      <td>row 16: 64
      <td>row 16: 125
      <td>row 16: 216
      <td>row 16: 343
    <tr>
      <td>row 17: 1
      <td>row 17: 8
      <td>row 17: 27
      <td>row 17: 64
      <td>row 17: 125
      <td>row 17: 216
      <td>row 17: 343
    <tr>
      <td>row 18: 1
      <td>row 18: 8
      <td>row 18: 27
      <td>row 18: 64
      <td>row 18: 125
      <td>row 18: 216
      <td>row 18: 343
    <tr>
      <td>row 19: 1
      <td>row 19: 8
      <td>row 19: 27
      <td>row 19: 64
      <td>row 19: 125
      <td>row 19: 216
      <td>row 19: 343
    <tr>
      <td>row 20: 1
      <td>row 20: 8
      <td>row 20: 27
      <td>row 20: 64
      <td>row 20: 125
      <td>row 20: 216
      <td>row 20: 343
table>
5. Mặc dù điều này cho phép cuộn, bảng mất một số tính toàn vẹn của nó và các ô bảng cố gắng trở nên nhỏ nhất có thể. Để giảm thiểu vấn đề này, chúng tôi đã đặt
<table>
  <thead>
    <tr>
      <th>1<sup>3sup> equals:
      <th>2<sup>3sup> equals:
      <th>3<sup>3sup> equals:
      <th>4<sup>3sup> equals:
      <th>5<sup>3sup> equals:
      <th>6<sup>3sup> equals:
      <th>7<sup>3sup> equals:
  <tbody>
    <tr>
      <td>row 1: 1
      <td>row 1: 8
      <td>row 1: 27
      <td>row 1: 64
      <td>row 1: 125
      <td>row 1: 216
      <td>row 1: 343
    <tr>
      <td>row 2: 1
      <td>row 2: 8
      <td>row 2: 27
      <td>row 2: 64
      <td>row 2: 125
      <td>row 2: 216
      <td>row 2: 343
    <tr>
      <td>row 3: 1
      <td>row 3: 8
      <td>row 3: 27
      <td>row 3: 64
      <td>row 3: 125
      <td>row 3: 216
      <td>row 3: 343
    <tr>
      <td>row 4: 1
      <td>row 4: 8
      <td>row 4: 27
      <td>row 4: 64
      <td>row 4: 125
      <td>row 4: 216
      <td>row 4: 343
    <tr>
      <td>row 5: 1
      <td>row 5: 8
      <td>row 5: 27
      <td>row 5: 64
      <td>row 5: 125
      <td>row 5: 216
      <td>row 5: 343
    <tr>
      <td>row 6: 1
      <td>row 6: 8
      <td>row 6: 27
      <td>row 6: 64
      <td>row 6: 125
      <td>row 6: 216
      <td>row 6: 343
    <tr>
      <td>row 7: 1
      <td>row 7: 8
      <td>row 7: 27
      <td>row 7: 64
      <td>row 7: 125
      <td>row 7: 216
      <td>row 7: 343
    <tr>
      <td>row 8: 1
      <td>row 8: 8
      <td>row 8: 27
      <td>row 8: 64
      <td>row 8: 125
      <td>row 8: 216
      <td>row 8: 343
    <tr>
      <td>row 9: 1
      <td>row 9: 8
      <td>row 9: 27
      <td>row 9: 64
      <td>row 9: 125
      <td>row 9: 216
      <td>row 9: 343
    <tr>
      <td>row 10: 1
      <td>row 10: 8
      <td>row 10: 27
      <td>row 10: 64
      <td>row 10: 125
      <td>row 10: 216
      <td>row 10: 343
    <tr>
      <td>row 11: 1
      <td>row 11: 8
      <td>row 11: 27
      <td>row 11: 64
      <td>row 11: 125
      <td>row 11: 216
      <td>row 11: 343
    <tr>
      <td>row 12: 1
      <td>row 12: 8
      <td>row 12: 27
      <td>row 12: 64
      <td>row 12: 125
      <td>row 12: 216
      <td>row 12: 343
    <tr>
      <td>row 13: 1
      <td>row 13: 8
      <td>row 13: 27
      <td>row 13: 64
      <td>row 13: 125
      <td>row 13: 216
      <td>row 13: 343
    <tr>
      <td>row 14: 1
      <td>row 14: 8
      <td>row 14: 27
      <td>row 14: 64
      <td>row 14: 125
      <td>row 14: 216
      <td>row 14: 343
    <tr>
      <td>row 15: 1
      <td>row 15: 8
      <td>row 15: 27
      <td>row 15: 64
      <td>row 15: 125
      <td>row 15: 216
      <td>row 15: 343
    <tr>
      <td>row 16: 1
      <td>row 16: 8
      <td>row 16: 27
      <td>row 16: 64
      <td>row 16: 125
      <td>row 16: 216
      <td>row 16: 343
    <tr>
      <td>row 17: 1
      <td>row 17: 8
      <td>row 17: 27
      <td>row 17: 64
      <td>row 17: 125
      <td>row 17: 216
      <td>row 17: 343
    <tr>
      <td>row 18: 1
      <td>row 18: 8
      <td>row 18: 27
      <td>row 18: 64
      <td>row 18: 125
      <td>row 18: 216
      <td>row 18: 343
    <tr>
      <td>row 19: 1
      <td>row 19: 8
      <td>row 19: 27
      <td>row 19: 64
      <td>row 19: 125
      <td>row 19: 216
      <td>row 19: 343
    <tr>
      <td>row 20: 1
      <td>row 20: 8
      <td>row 20: 27
      <td>row 20: 64
      <td>row 20: 125
      <td>row 20: 216
      <td>row 20: 343
table>
6 thành
<table>
  <thead>
    <tr>
      <th>1<sup>3sup> equals:
      <th>2<sup>3sup> equals:
      <th>3<sup>3sup> equals:
      <th>4<sup>3sup> equals:
      <th>5<sup>3sup> equals:
      <th>6<sup>3sup> equals:
      <th>7<sup>3sup> equals:
  <tbody>
    <tr>
      <td>row 1: 1
      <td>row 1: 8
      <td>row 1: 27
      <td>row 1: 64
      <td>row 1: 125
      <td>row 1: 216
      <td>row 1: 343
    <tr>
      <td>row 2: 1
      <td>row 2: 8
      <td>row 2: 27
      <td>row 2: 64
      <td>row 2: 125
      <td>row 2: 216
      <td>row 2: 343
    <tr>
      <td>row 3: 1
      <td>row 3: 8
      <td>row 3: 27
      <td>row 3: 64
      <td>row 3: 125
      <td>row 3: 216
      <td>row 3: 343
    <tr>
      <td>row 4: 1
      <td>row 4: 8
      <td>row 4: 27
      <td>row 4: 64
      <td>row 4: 125
      <td>row 4: 216
      <td>row 4: 343
    <tr>
      <td>row 5: 1
      <td>row 5: 8
      <td>row 5: 27
      <td>row 5: 64
      <td>row 5: 125
      <td>row 5: 216
      <td>row 5: 343
    <tr>
      <td>row 6: 1
      <td>row 6: 8
      <td>row 6: 27
      <td>row 6: 64
      <td>row 6: 125
      <td>row 6: 216
      <td>row 6: 343
    <tr>
      <td>row 7: 1
      <td>row 7: 8
      <td>row 7: 27
      <td>row 7: 64
      <td>row 7: 125
      <td>row 7: 216
      <td>row 7: 343
    <tr>
      <td>row 8: 1
      <td>row 8: 8
      <td>row 8: 27
      <td>row 8: 64
      <td>row 8: 125
      <td>row 8: 216
      <td>row 8: 343
    <tr>
      <td>row 9: 1
      <td>row 9: 8
      <td>row 9: 27
      <td>row 9: 64
      <td>row 9: 125
      <td>row 9: 216
      <td>row 9: 343
    <tr>
      <td>row 10: 1
      <td>row 10: 8
      <td>row 10: 27
      <td>row 10: 64
      <td>row 10: 125
      <td>row 10: 216
      <td>row 10: 343
    <tr>
      <td>row 11: 1
      <td>row 11: 8
      <td>row 11: 27
      <td>row 11: 64
      <td>row 11: 125
      <td>row 11: 216
      <td>row 11: 343
    <tr>
      <td>row 12: 1
      <td>row 12: 8
      <td>row 12: 27
      <td>row 12: 64
      <td>row 12: 125
      <td>row 12: 216
      <td>row 12: 343
    <tr>
      <td>row 13: 1
      <td>row 13: 8
      <td>row 13: 27
      <td>row 13: 64
      <td>row 13: 125
      <td>row 13: 216
      <td>row 13: 343
    <tr>
      <td>row 14: 1
      <td>row 14: 8
      <td>row 14: 27
      <td>row 14: 64
      <td>row 14: 125
      <td>row 14: 216
      <td>row 14: 343
    <tr>
      <td>row 15: 1
      <td>row 15: 8
      <td>row 15: 27
      <td>row 15: 64
      <td>row 15: 125
      <td>row 15: 216
      <td>row 15: 343
    <tr>
      <td>row 16: 1
      <td>row 16: 8
      <td>row 16: 27
      <td>row 16: 64
      <td>row 16: 125
      <td>row 16: 216
      <td>row 16: 343
    <tr>
      <td>row 17: 1
      <td>row 17: 8
      <td>row 17: 27
      <td>row 17: 64
      <td>row 17: 125
      <td>row 17: 216
      <td>row 17: 343
    <tr>
      <td>row 18: 1
      <td>row 18: 8
      <td>row 18: 27
      <td>row 18: 64
      <td>row 18: 125
      <td>row 18: 216
      <td>row 18: 343
    <tr>
      <td>row 19: 1
      <td>row 19: 8
      <td>row 19: 27
      <td>row 19: 64
      <td>row 19: 125
      <td>row 19: 216
      <td>row 19: 343
    <tr>
      <td>row 20: 1
      <td>row 20: 8
      <td>row 20: 27
      <td>row 20: 64
      <td>row 20: 125
      <td>row 20: 216
      <td>row 20: 343
table>
7 trên
HTMLTableSectionElement.prototype.sort = function (cb) {
  Array.from(this.rows)
    .sort(cb)
    .forEach((e) => this.appendChild(this.removeChild(e)));
};

document
  .querySelector("table")
  .tBodies[0].sort((a, b) => a.textContent.localeCompare(b.textContent));
3. Tuy nhiên, chúng tôi không làm điều này cho
HTMLTableSectionElement.prototype.sort = function (cb) {
  Array.from(this.rows)
    .sort(cb)
    .forEach((e) => this.appendChild(this.removeChild(e)));
};

document
  .querySelector("table")
  .tBodies[0].sort((a, b) => a.textContent.localeCompare(b.textContent));
2 để tránh các tiêu đề dài buộc các cột phải rộng hơn chúng cần phải hiển thị dữ liệu.

  • Để giữ các tiêu đề bảng trên trang trong khi cuộn xuống, chúng tôi đã đặt
    table,
    th,
    td {
      border: 1px solid;
    }
    
    table {
      width: 100%;
      max-width: 400px;
      height: 240px;
      margin: 0 auto;
      display: block;
      overflow-x: auto;
      border-spacing: 0;
    }
    
    tbody {
      white-space: nowrap;
    }
    
    th,
    td {
      padding: 5px 10px;
      border-top-width: 0;
      border-left-width: 0;
    }
    
    th {
      position: sticky;
      top: 0;
      background: #fff;
      vertical-align: bottom;
    }
    
    th:last-child,
    td:last-child {
      border-right-width: 0;
    }
    
    tr:last-child td {
      border-bottom-width: 0;
    }
    
    0 thành các yếu tố
    const allTables = document.querySelectorAll("table");
    
    for (const table of allTables) {
      const tBody = table.tBodies[0];
      const rows = Array.from(tBody.rows);
      const headerCells = table.tHead.rows[0].cells;
    
      for (const th of headerCells) {
        const cellIndex = th.cellIndex;
    
        th.addEventListener("click", () => {
          rows.sort((tr1, tr2) => {
            const tr1Text = tr1.cells[cellIndex].textContent;
            const tr2Text = tr2.cells[cellIndex].textContent;
            return tr1Text.localeCompare(tr2Text);
          });
    
          tBody.append(...rows);
        });
      }
    }
    
    8. Lưu ý rằng chúng tôi chưa đặt
    table {
      border-collapse: collapse;
      border-spacing: 0px;
    }
    table,
    th,
    td {
      padding: 5px;
      border: 1px solid black;
    }
    
    7 thành
    table,
    th,
    td {
      border: 1px solid;
    }
    
    table {
      width: 100%;
      max-width: 400px;
      height: 240px;
      margin: 0 auto;
      display: block;
      overflow-x: auto;
      border-spacing: 0;
    }
    
    tbody {
      white-space: nowrap;
    }
    
    th,
    td {
      padding: 5px 10px;
      border-top-width: 0;
      border-left-width: 0;
    }
    
    th {
      position: sticky;
      top: 0;
      background: #fff;
      vertical-align: bottom;
    }
    
    th:last-child,
    td:last-child {
      border-right-width: 0;
    }
    
    tr:last-child td {
      border-bottom-width: 0;
    }
    
    3, vì chúng tôi thực hiện tiêu đề không thể tách chính xác khỏi phần còn lại của bảng.
  • Mối quan tâm tiếp cận

Chú thích

Bằng cách cung cấp một yếu tố

<table>
  <thead>
    <tr>
      <th>Numbersth>
      <th>Lettersth>
    tr>
  thead>
  <tbody>
    <tr>
      <td>3td>
      <td>Atd>
    tr>
    <tr>
      <td>2td>
      <td>Btd>
    tr>
    <tr>
      <td>1td>
      <td>Ctd>
    tr>
  tbody>
table>
7 có giá trị mô tả rõ ràng và chính xác mục đích của bảng, nó giúp mọi người quyết định xem họ có cần đọc phần còn lại của nội dung bảng hay bỏ qua nó không.

Điều này giúp mọi người điều hướng với sự trợ giúp của công nghệ hỗ trợ như người đọc màn hình, những người trải qua điều kiện tầm nhìn thấp và những người có mối quan tâm nhận thức.

<table>
  <caption>
    Color names and values
  caption>
  <tbody>
    <tr>
      <th scope="col">Nameth>
      <th scope="col">HEXth>
      <th scope="col">HSLath>
      <th scope="col">RGBath>
    tr>
    <tr>
      <th scope="row">Tealth>
      <td><code>#51F6F6code>td>
      <td><code>hsla(180, 90%, 64%, 1)code>td>
      <td><code>rgba(81, 246, 246, 1)code>td>
    tr>
    <tr>
      <th scope="row">Goldenrodth>
      <td><code>#F6BC57code>td>
      <td><code>hsla(38, 90%, 65%, 1)code>td>
      <td><code>rgba(246, 188, 87, 1)code>td>
    tr>
  tbody>
table>

Mdn thêm chú thích vào bảng của bạn với

  • Chú thích & Tóm tắt • Bảng • Hướng dẫn truy cập web W3C WAI
  • Các hàng và cột phạm vi
  • Thuộc tính
    table,
    th,
    td {
      border: 1px solid;
    }
    
    table {
      width: 100%;
      max-width: 400px;
      height: 240px;
      margin: 0 auto;
      display: block;
      overflow-x: auto;
      border-spacing: 0;
    }
    
    tbody {
      white-space: nowrap;
    }
    
    th,
    td {
      padding: 5px 10px;
      border-top-width: 0;
      border-left-width: 0;
    }
    
    th {
      position: sticky;
      top: 0;
      background: #fff;
      vertical-align: bottom;
    }
    
    th:last-child,
    td:last-child {
      border-right-width: 0;
    }
    
    tr:last-child td {
      border-bottom-width: 0;
    }
    
    5 trên các phần tử tiêu đề là dự phòng trong các bối cảnh đơn giản, bởi vì phạm vi được suy ra. Tuy nhiên, một số công nghệ hỗ trợ có thể không rút ra các suy luận chính xác, do đó chỉ định phạm vi tiêu đề có thể cải thiện trải nghiệm người dùng. Trong các bảng phức tạp, phạm vi có thể được chỉ định để cung cấp thông tin cần thiết về các ô liên quan đến tiêu đề.
  • Thí dụ

Cung cấp khai báo table, th, td { border: 1px solid; } table { width: 100%; max-width: 400px; height: 240px; margin: 0 auto; display: block; overflow-x: auto; border-spacing: 0; } tbody { white-space: nowrap; } th, td { padding: 5px 10px; border-top-width: 0; border-left-width: 0; } th { position: sticky; top: 0; background: #fff; vertical-align: bottom; } th:last-child, td:last-child { border-right-width: 0; } tr:last-child td { border-bottom-width: 0; } 6 trên phần tử const allTables = document.querySelectorAll("table"); for (const table of allTables) { const tBody = table.tBodies[0]; const rows = Array.from(tBody.rows); const headerCells = table.tHead.rows[0].cells; for (const th of headerCells) { const cellIndex = th.cellIndex; th.addEventListener("click", () => { rows.sort((tr1, tr2) => { const tr1Text = tr1.cells[cellIndex].textContent; const tr2Text = tr2.cells[cellIndex].textContent; return tr1Text.localeCompare(tr2Text); }); tBody.append(...rows); }); } } 8 sẽ giúp mô tả rằng ô ở đầu cột. Cung cấp một tuyên bố table, th, td { border: 1px solid; } table { width: 100%; max-width: 400px; height: 240px; margin: 0 auto; display: block; overflow-x: auto; border-spacing: 0; } tbody { white-space: nowrap; } th, td { padding: 5px 10px; border-top-width: 0; border-left-width: 0; } th { position: sticky; top: 0; background: #fff; vertical-align: bottom; } th:last-child, td:last-child { border-right-width: 0; } tr:last-child td { border-bottom-width: 0; } 8 trên phần tử const allTables = document.querySelectorAll("table"); for (const table of allTables) { const tBody = table.tBodies[0]; const rows = Array.from(tBody.rows); const headerCells = table.tHead.rows[0].cells; for (const th of headerCells) { const cellIndex = th.cellIndex; th.addEventListener("click", () => { rows.sort((tr1, tr2) => { const tr1Text = tr1.cells[cellIndex].textContent; const tr2Text = tr2.cells[cellIndex].textContent; return tr1Text.localeCompare(tr2Text); }); tBody.append(...rows); }); } } 8 sẽ giúp mô tả rằng ô là lần đầu tiên liên tiếp.

Bảng MDN cho người dùng khiếm thị

Các bảng có hai tiêu đề • Bảng • Hướng dẫn truy cập web W3C WAI

Các bảng có tiêu đề không đều • Bảng • Hướng dẫn truy cập web W3C WAI

  • Chú thích & Tóm tắt • Bảng • Hướng dẫn truy cập web W3C WAI
  • Các hàng và cột phạm vi
  • Thuộc tính
    table,
    th,
    td {
      border: 1px solid;
    }
    
    table {
      width: 100%;
      max-width: 400px;
      height: 240px;
      margin: 0 auto;
      display: block;
      overflow-x: auto;
      border-spacing: 0;
    }
    
    tbody {
      white-space: nowrap;
    }
    
    th,
    td {
      padding: 5px 10px;
      border-top-width: 0;
      border-left-width: 0;
    }
    
    th {
      position: sticky;
      top: 0;
      background: #fff;
      vertical-align: bottom;
    }
    
    th:last-child,
    td:last-child {
      border-right-width: 0;
    }
    
    tr:last-child td {
      border-bottom-width: 0;
    }
    
    5 trên các phần tử tiêu đề là dự phòng trong các bối cảnh đơn giản, bởi vì phạm vi được suy ra. Tuy nhiên, một số công nghệ hỗ trợ có thể không rút ra các suy luận chính xác, do đó chỉ định phạm vi tiêu đề có thể cải thiện trải nghiệm người dùng. Trong các bảng phức tạp, phạm vi có thể được chỉ định để cung cấp thông tin cần thiết về các ô liên quan đến tiêu đề.

Thí dụ

Cung cấp khai báo
table,
th,
td {
  border: 1px solid;
}

table {
  width: 100%;
  max-width: 400px;
  height: 240px;
  margin: 0 auto;
  display: block;
  overflow-x: auto;
  border-spacing: 0;
}

tbody {
  white-space: nowrap;
}

th,
td {
  padding: 5px 10px;
  border-top-width: 0;
  border-left-width: 0;
}

th {
  position: sticky;
  top: 0;
  background: #fff;
  vertical-align: bottom;
}

th:last-child,
td:last-child {
  border-right-width: 0;
}

tr:last-child td {
  border-bottom-width: 0;
}
6 trên phần tử
const allTables = document.querySelectorAll("table");

for (const table of allTables) {
  const tBody = table.tBodies[0];
  const rows = Array.from(tBody.rows);
  const headerCells = table.tHead.rows[0].cells;

  for (const th of headerCells) {
    const cellIndex = th.cellIndex;

    th.addEventListener("click", () => {
      rows.sort((tr1, tr2) => {
        const tr1Text = tr1.cells[cellIndex].textContent;
        const tr2Text = tr2.cells[cellIndex].textContent;
        return tr1Text.localeCompare(tr2Text);
      });

      tBody.append(...rows);
    });
  }
}
8 sẽ giúp mô tả rằng ô ở đầu cột. Cung cấp một tuyên bố
table,
th,
td {
  border: 1px solid;
}

table {
  width: 100%;
  max-width: 400px;
  height: 240px;
  margin: 0 auto;
  display: block;
  overflow-x: auto;
  border-spacing: 0;
}

tbody {
  white-space: nowrap;
}

th,
td {
  padding: 5px 10px;
  border-top-width: 0;
  border-left-width: 0;
}

th {
  position: sticky;
  top: 0;
  background: #fff;
  vertical-align: bottom;
}

th:last-child,
td:last-child {
  border-right-width: 0;
}

tr:last-child td {
  border-bottom-width: 0;
}
8 trên phần tử
const allTables = document.querySelectorAll("table");

for (const table of allTables) {
  const tBody = table.tBodies[0];
  const rows = Array.from(tBody.rows);
  const headerCells = table.tHead.rows[0].cells;

  for (const th of headerCells) {
    const cellIndex = th.cellIndex;

    th.addEventListener("click", () => {
      rows.sort((tr1, tr2) => {
        const tr1Text = tr1.cells[cellIndex].textContent;
        const tr2Text = tr2.cells[cellIndex].textContent;
        return tr1Text.localeCompare(tr2Text);
      });

      tBody.append(...rows);
    });
  }
}
8 sẽ giúp mô tả rằng ô là lần đầu tiên liên tiếp.
Bảng MDN cho người dùng khiếm thị
# the-table-element

Các bảng có hai tiêu đề • Bảng • Hướng dẫn truy cập web W3C WAI

Các bảng có tiêu đề không đều • Bảng • Hướng dẫn truy cập web W3C WAI

H63: Sử dụng thuộc tính phạm vi để liên kết các ô tiêu đề và ô dữ liệu trong bảng dữ liệu | Kỹ thuật W3C cho WCAG 2.0

  • Bảng phức tạp
  • Công nghệ hỗ trợ như đầu đọc màn hình có thể gặp khó khăn trong các bảng phân tích cú pháp phức tạp đến mức các ô tiêu đề không thể được liên kết theo cách ngang hoặc dọc. Điều này thường được biểu thị bằng sự hiện diện của các thuộc tính
    <table>
      <caption>
        Color names and values
      caption>
      <tbody>
        <tr>
          <th scope="col">Nameth>
          <th scope="col">HEXth>
          <th scope="col">HSLath>
          <th scope="col">RGBath>
        tr>
        <tr>
          <th scope="row">Tealth>
          <td><code>#51F6F6code>td>
          <td><code>hsla(180, 90%, 64%, 1)code>td>
          <td><code>rgba(81, 246, 246, 1)code>td>
        tr>
        <tr>
          <th scope="row">Goldenrodth>
          <td><code>#F6BC57code>td>
          <td><code>hsla(38, 90%, 65%, 1)code>td>
          <td><code>rgba(246, 188, 87, 1)code>td>
        tr>
      tbody>
    table>
    
    0 và
    <table>
      <caption>
        Color names and values
      caption>
      <tbody>
        <tr>
          <th scope="col">Nameth>
          <th scope="col">HEXth>
          <th scope="col">HSLath>
          <th scope="col">RGBath>
        tr>
        <tr>
          <th scope="row">Tealth>
          <td><code>#51F6F6code>td>
          <td><code>hsla(180, 90%, 64%, 1)code>td>
          <td><code>rgba(81, 246, 246, 1)code>td>
        tr>
        <tr>
          <th scope="row">Goldenrodth>
          <td><code>#F6BC57code>td>
          <td><code>hsla(38, 90%, 65%, 1)code>td>
          <td><code>rgba(246, 188, 87, 1)code>td>
        tr>
      tbody>
    table>
    
    1.
    • Lý tưởng nhất, hãy xem xét các cách thay thế để trình bày nội dung của bảng, bao gồm chia nó thành một tập hợp các bảng nhỏ hơn, liên quan không phải dựa vào việc sử dụng các thuộc tính
      <table>
        <caption>
          Color names and values
        caption>
        <tbody>
          <tr>
            <th scope="col">Nameth>
            <th scope="col">HEXth>
            <th scope="col">HSLath>
            <th scope="col">RGBath>
          tr>
          <tr>
            <th scope="row">Tealth>
            <td><code>#51F6F6code>td>
            <td><code>hsla(180, 90%, 64%, 1)code>td>
            <td><code>rgba(81, 246, 246, 1)code>td>
          tr>
          <tr>
            <th scope="row">Goldenrodth>
            <td><code>#F6BC57code>td>
            <td><code>hsla(38, 90%, 65%, 1)code>td>
            <td><code>rgba(246, 188, 87, 1)code>td>
          tr>
        tbody>
      table>
      
      0 và
      <table>
        <caption>
          Color names and values
        caption>
        <tbody>
          <tr>
            <th scope="col">Nameth>
            <th scope="col">HEXth>
            <th scope="col">HSLath>
            <th scope="col">RGBath>
          tr>
          <tr>
            <th scope="row">Tealth>
            <td><code>#51F6F6code>td>
            <td><code>hsla(180, 90%, 64%, 1)code>td>
            <td><code>rgba(81, 246, 246, 1)code>td>
          tr>
          <tr>
            <th scope="row">Goldenrodth>
            <td><code>#F6BC57code>td>
            <td><code>hsla(38, 90%, 65%, 1)code>td>
            <td><code>rgba(246, 188, 87, 1)code>td>
          tr>
        tbody>
      table>
      
      1. Ngoài việc giúp những người sử dụng công nghệ hỗ trợ hiểu nội dung của bảng, điều này cũng có thể mang lại lợi ích cho những người có mối quan tâm nhận thức, những người có thể gặp khó khăn trong việc hiểu các hiệp hội mà bố cục bảng đang mô tả.
    • Nếu bảng không thể bị phá vỡ, hãy sử dụng kết hợp các thuộc tính
      <table>
        <caption>
          Color names and values
        caption>
        <tbody>
          <tr>
            <th scope="col">Nameth>
            <th scope="col">HEXth>
            <th scope="col">HSLath>
            <th scope="col">RGBath>
          tr>
          <tr>
            <th scope="row">Tealth>
            <td><code>#51F6F6code>td>
            <td><code>hsla(180, 90%, 64%, 1)code>td>
            <td><code>rgba(81, 246, 246, 1)code>td>
          tr>
          <tr>
            <th scope="row">Goldenrodth>
            <td><code>#F6BC57code>td>
            <td><code>hsla(38, 90%, 65%, 1)code>td>
            <td><code>rgba(246, 188, 87, 1)code>td>
          tr>
        tbody>
      table>
      
      4 và
      <table>
        <caption>
          Color names and values
        caption>
        <tbody>
          <tr>
            <th scope="col">Nameth>
            <th scope="col">HEXth>
            <th scope="col">HSLath>
            <th scope="col">RGBath>
          tr>
          <tr>
            <th scope="row">Tealth>
            <td><code>#51F6F6code>td>
            <td><code>hsla(180, 90%, 64%, 1)code>td>
            <td><code>rgba(81, 246, 246, 1)code>td>
          tr>
          <tr>
            <th scope="row">Goldenrodth>
            <td><code>#F6BC57code>td>
            <td><code>hsla(38, 90%, 65%, 1)code>td>
            <td><code>rgba(246, 188, 87, 1)code>td>
          tr>
        tbody>
      table>
      
      5 để liên kết lập trình từng ô bảng với (các) tiêu đề mà ô được liên kết với.
    • Các bảng có tiêu đề đa cấp • Bảng • Hướng dẫn truy cập web W3C WAI
    • H43: Sử dụng các thuộc tính ID và tiêu đề để liên kết các ô dữ liệu với các ô tiêu đề trong bảng dữ liệu | Kỹ thuật cho W3C WCAG 2.0

Những yếu tố nào được sử dụng để tạo bảng?

11.2 Các yếu tố để xây dựng bảng..
1 Phần tử bảng ..
2 Chú thích bảng: Phần tử chú thích ..
3 Nhóm hàng: Các yếu tố, tfoot và tbody của TOAD, tfoot ..
4 Nhóm cột: Các phần tử của nhóm và col ..
5 Hàng bảng: Phần tử TR ..
6 ô bảng: Các phần tử TH và TD ..
11.3. 1 biên giới và quy tắc ..

Làm thế nào bạn có thể tạo các bảng trong HTML?

Một bảng HTML được tạo với thẻ mở và thẻ đóng.Bên trong các thẻ này, dữ liệu được sắp xếp thành các hàng và cột bằng cách sử dụng các thẻ hàng mở và đóng bảng và mở và đóng các thẻ dữ liệu bảng.Thẻ hàng bảng được sử dụng để tạo một hàng dữ liệu.with an opening tag and a closing
tag
. Inside these tags, data is organized into rows and columns by using opening and closing table row
tags. Table row