javascript - Table Tag Not Nesting Inside P Tags In DOM - Stack Overflow

I have been playin with dynamic changes to innerHTML content and have noticed some strange behaviour wi

I have been playin with dynamic changes to innerHTML content and have noticed some strange behaviour with tables nested inside other elements.

For example form p /p p table /table /p /form was not seeing the table as innerHTML of the second p but instead listed the table as a sibling of that p tag rather than a child.

I figure this is a well known behaviour.

What is the gap in my knowlegde?

TIA

I have been playin with dynamic changes to innerHTML content and have noticed some strange behaviour with tables nested inside other elements.

For example form p /p p table /table /p /form was not seeing the table as innerHTML of the second p but instead listed the table as a sibling of that p tag rather than a child.

I figure this is a well known behaviour.

What is the gap in my knowlegde?

TIA

Share Improve this question edited Sep 18, 2011 at 5:35 mu is too short 435k71 gold badges859 silver badges818 bronze badges asked Sep 18, 2011 at 5:24 Muzza MuzzaMuzza Muzza 431 silver badge3 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

You can't put a <table> inside a <p>. From the HTML4 specification:

<!ELEMENT P - O (%inline;)* -- paragraph -->
[...]
It cannot contain block-level elements (including P itself).

And then if you look at what the %inline; elements are, you won't find <table> in the list.

And for HTML5, <p> can contain phrasing content:

Permitted contents

Phrasing content

And phrasing content is character data and phrasing elements, phrasing elements are:

a or em or strong or small ... meter

There's no <table> in that list.

So you're trying to insert invalid HTML and the browser is changing <p><table></table></p> into <p></p><table></table> (i.e. moving the table child up to a sibling) in order to get valid HTML.

If you give the browser invalid HTML, the browser will guess what you really mean and go with its guess.

This is because both <p> and <table> are block-level elements.

Use a container element for your table, like a <div>.

Good luck! :)

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745432976a4627476.html

相关推荐

  • javascript - Table Tag Not Nesting Inside P Tags In DOM - Stack Overflow

    I have been playin with dynamic changes to innerHTML content and have noticed some strange behaviour wi

    5小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信