Html 为什么 <table> 不允许在 <p> 中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10086912/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 23:41:40  来源:igfitidea点击:

Why is <table> not allowed inside <p>

htmlasp.netvisual-studio-2008html-table

提问by Rohit Vipin Mathews

Why can't <p>be nested inside <table>? What is the correction I could make? Removing the <div>and <p>tags corrupt my design. As the website follows a client provided design.

为什么不能<p>嵌套在里面<table>?我可以做哪些更正?删除<div><p>标签破坏了我的设计。由于网站遵循客户提供的设计。

I have the following code and it works fine in VS2008, but I get warnings:

我有以下代码,它在 VS2008 中工作正常,但我收到警告:

<div class="right_articles">
    <p>
        <table>
            <tr>
                <td>
                    <img alt="Img not found" src="images/ribbon.gif" style="width: 155px; height: 125px;" />
                </td>
                <td>
                    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label><br />
                    <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label><br />
                    <asp:Label ID="Label3" runat="server" Text="Label"></asp:Label>
                </td>
            </tr>
        </table> 
    </p>
    <p>&nbsp;</p>
    <p>
        <table>
            <tr>
                <td>
                    <img alt="Img not found" src="images/medal.gif" style="width: 155px; height: 125px;" />
                </td>
                <td>          
                    <asp:Label ID="Label4" runat="server" Text="Label"></asp:Label><br />
                    <asp:Label ID="Label5" runat="server" Text="Label"></asp:Label><br />
                    <asp:Label ID="Label6" runat="server" Text="Label"></asp:Label>      
                </td>
            </tr>
        </table>
    </p>
</div>

Warning 1 This end tag has no matching start tag. E:\WebSite4\test.master 121 Warning 2 Validation (XHTML 1.0 Transitional): Text is not allowed between the opening and closing tags for element html'. E:\WebSite4\test.master 5 Warning 3 Validation (XHTML 1.0 Transitional): Element 'form' is missing its closing tag. E:\WebSite4\test.master 21 Warning 4 The class or CssClass value is not defined. E:\WebSite4\test.master 33 Warning 5 File 'spacer.gif' was not found. E:\WebSite4\test.master 116 Warning 7 Validation (XHTML 1.0 Transitional): Element 'img' is missing required attribute 'alt'. E:\WebSite4\test.master 116 Warning 8 Validation (XHTML 1.0 Transitional): Element 'table' cannot be nested within element 'p'. E:\WebSite4\test.master 78 Warning 9 Validation (XHTML 1.0 Transitional): Element 'table' cannot be nested within element 'p'. E:\WebSite4\test.master 93

警告 1 此结束标记没有匹配的开始标记。E:\WebSite4\test.master 121 警告 2 验证(XHTML 1.0 过渡):元素 html' 的开始和结束标记之间不允许有文本。E:\WebSite4\test.master 5 警告 3 验证(XHTML 1.0 Transitional):元素“form”缺少其结束标记。E:\WebSite4\test.master 21 警告 4 未定义类或 CssClass 值。E:\WebSite4\test.master 33 警告 5 找不到文件“spacer.gif”。E:\WebSite4\test.master 116 警告 7 验证(XHTML 1.0 过渡):元素 'img' 缺少必需的属性 'alt'。E:\WebSite4\test.master 116 警告 8 验证(XHTML 1.0 Transitional):元素“table”不能嵌套在元素“p”中。E:\WebSite4\test.master 78 警告 9 验证 (XHTML 1. 0 Transitional):元素“table”不能嵌套在元素“p”中。E:\WebSite4\test.master 93

回答by Tim Schmelter

In HTML it's important to understand that Pelementscannot contain other block level elements and TABLE is a block level element. The Pclosing tag is optional and when you try to make a Pelement contain something that it cannot contain, the closing Ptag is assumed by the browser.

在 HTML 中,重要的是要了解P元素不能包含其他块级元素,而 TABLE 是块级元素。该P结束标记是可选的,当你试图做一个P元素包含的东西,它不能包含,关闭P标签是由浏览器承担。

The P element represents a paragraph. It cannot contain block-level elements (including P itself).

P 元素代表一个段落。它不能包含块级元素(包括 P 本身)。

http://www.w3.org/TR/html401/struct/text.html#h-9.3.1

http://www.w3.org/TR/html401/struct/text.html#h-9.3.1

回答by DigiDamsel

In answer to your actual question a paragraph cannot contain any other block elements, which includes tables. Also in addition to this the closing </p>tag is optional so the first closing tag that is subsequently found by the parser will deem to have closed the paragraph.

在回答您的实际问题时,段落不能包含任何其他块元素,其中包括表格。此外,结束</p>标记是可选的,因此解析器随后找到的第一个结束标记将视为已结束段落。

It would help if I could see more of the code and layout, however I believe that removing the <p>tags from around the tables and then correctly formatting the positioning of your tables using CSS should achieve your results.

如果我能看到更多的代码和布局会有所帮助,但是我相信<p>从表格周围删除标签,然后使用 CSS 正确格式化表格的位置应该可以达到您的结果。

<div class="right_articles">
            <table>
            <tr>
            <td>
                <img alt="Img not found" src="images/ribbon.gif"                     
                    style="width: 155px; height: 125px;" />
            </td>
            <td>
                <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label><br />
                <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label><br />
                <asp:Label ID="Label3" runat="server" Text="Label"></asp:Label>
            </td>
            </tr>
            </table>
            <p>&nbsp;</p>
            <table>
            <tr>
            <td>
                <img alt="Img not found" src="images/medal.gif"                     
                    style="width: 155px; height: 125px;" />
            </td>
            <td>          
                <asp:Label ID="Label4" runat="server" Text="Label"></asp:Label><br />
                <asp:Label ID="Label5" runat="server" Text="Label"></asp:Label><br />
                <asp:Label ID="Label6" runat="server" Text="Label"></asp:Label>      
            </td>
            </tr>
            </table>
        </div>