Is it necessary to have in any table?

后端 未结 7 751
天涯浪人
天涯浪人 2021-01-14 16:10

is it necessary to have in any table? even if table has no heading?

table has 3 other tag

7条回答
  •  青春惊慌失措
    2021-01-14 16:25

    According to the HTML DTD this is the content model for HTML tables:

    
    
    
    
    
    
    
    
    
    

    So this is illegal syntax:

    Heading of table
    

    It should be:

    Heading of table
    

    elements aren't required anywhere. They're simply one of the two cell types (the other being ) that you can use in a table row. A is an optional table section that can contain one or more rows.

    Edit: As to why to use there are several reasons:

    1. Semantic: You're differentiating between the content of your table and "metadata". This is most often used to delineate between column headers and data rows;
    2. Accessibility: it helps people who use screen readers to understand the contents of the table;
    3. Non-Screen Media: Printing a multi-page table may allow you to put the contents at the top of each page so people can understand what the columns meaning without flicking back several pages;
    4. Styling: CSS can be applied to elements, elements, both or some other combination. It gives you something else to write a selector against;
    5. Javascript: this often comes up when using jQuery and similar libraries. The extra information is helpful in writing code.

    As an example of (5) you might do this:

    $("table > tbody > tr:nth-child(odd)").addClass("odd");
    

    The element means those rows won't be styled that way. Or you might do:

    $("table > tbody > tr").hover(function() {
      $(this).addClass("hover");
    }, function() {
      $(this).removeClass("hover");
    });
    

    with:

    tr.hover { background: yellow; }
    

    which again excludes the rows.

    Lastly, many of these same arguments apply to using elements over elements: you're indicating that this cell isn't data but a header of some kind. Often such cells will be grouped together in one or more rows in the section or be the first cell in each row depending on the structure and nature of your table.

提交回复
热议问题