Jquery: Table sub-label tbody

Source: Internet
Author: User
<table><tr><td>1</td></tr><tr><td>2</td></tr></table>

The above is a simple HTML code, and such a table is used in the work.

<table><tr><th>1</th></tr><tbody></tbody></table>

I want to use jquery to dynamically operate tbody HTML ();

Then I wrote the following code:

1 var html = "<tr> <TD> I am tbody </TD> </tr>"; 2 3 $ ("Table tbody" ).html (HTML );

Then I found the page changed

<Table> <tr> <TD> I am tbody </TD> </tr> <tbody> </table>

After some tests, we found that using js to get the tbody of a table and using the child element tbody of the table may not necessarily get the expected results.

Because the table tag has a tbody by default. The tbody is all the tr values in the table.

So when we do not write tbody or thead In the table

$ ("Table tbody") is equivalent to $ ("table ");
If we write only the tbody tag in the table and do not include all the tr tags, $ ("Table tbody") returns the opposite result, that is, the above Code will exclude the content in the self-declared tbody and obtain the content outside the tbody in the table.

Okay! Simple question. The solution is also simple:
<table><thead><tr><th>head</th></tr></thead><tbody><tr><td>body</td></tr></tbody></table>

Do not be lazy. Write the entire thead tbody in a standard way and there will be no problems @@




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.