What DOM elements can be children of tr?

I have a problem because I want to surround some of td inside tr , so I need to know.

What DOM elements can be children of tr? (I know that div cannot.)

+7
source share
3 answers

W3C specify this material. For tr you can find it here . In principle, only the elements th and td can be the direct contents of tr .

If you need other material inside your table, it must go into the td or th elements. For example, td ( link here ) may contain stream elements , including div ( link here ).

+10
source

The HTML specification states:

Allowed Content

Zero or more: one td element or one th element

<tr> elements can only contain <td> and <th> s.

+4
source

If the question is about a DOM tree built from an HTML document that conforms to the HTML specifications, then the answer will be td and th , as explained in paxdiablos answer.

But you can change the DOM tree as you like in scripts. For example:

 <!doctype html> <table border> <tr><td>foo <td>bar </table> <script> var p = document.createElement('div'); p.innerHTML = 'Hello world!'; document.getElementsByTagName('tr')[0].appendChild(p); </script> 

This will make the div element a child of the tr element. You can even create multiple td elements and make them children of the div element.

Whether this makes sense and whether it can confuse browsers is another problem. And browsers will not parse ordinary HTML markup, where, for example, the tr element contains a div element as a child element - parsers are not ready to handle such cases. Grouping td elements (in any other way than creating their child tr elements) is not possible with HTML markup. But in the DOM, you can create odd trees.

If you want to deal with some td elements in a special way, give them a class .

0
source

All Articles