V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Dive into HTML5
http://diveintohtml5.org/
lisisi
V2EX  ›  HTML

<p>包不住<div>是强制的 w3c 标准?还是约定成俗的规范?

  •  
  •   lisisi · 2020-09-22 14:32:29 +08:00 · 2662 次点击
    这是一个创建于 1284 天前的主题,其中的信息可能已经有所发展或是发生改变。
    发现有些浏览器会把<p>中的<div>吐出来,有些不吐。
    4 条回复    2020-09-22 16:36:09 +08:00
    mokeyjay
        1
    mokeyjay  
       2020-09-22 14:36:05 +08:00
    没看懂,举个例子?
    codehz
        2
    codehz  
       2020-09-22 15:00:13 +08:00   ❤️ 3
    p 的内容必须是 flow content ( https://html.spec.whatwg.org/#flow-content
    而 div 是 Palpable content ( https://html.spec.whatwg.org/#palpable-content
    所以 p 里面不能放 div,
    至于为什么 div 会被吐出来,就是因为根据 https://html.spec.whatwg.org/#concept-element-tag-omission 规则,元素结束标签在一些情况可以省略,而多余的结束标签会被忽略
    p 的结束标签可以省略
    https://html.spec.whatwg.org/#the-p-element

    Tag omission in text/html:
    A p element's end tag can be omitted if the p element is immediately followed by an address, article, aside, blockquote, details, div, dl, fieldset, figcaption, figure, footer, form, h1, h2, h3, h4, h5, h6, header, hgroup, hr, main, menu, nav, ol, p, pre, section, table, or ul element, or if there is no more content in the parent element and the parent element is an HTML element that is not an a, audio, del, ins, map, noscript, or video element, or an autonomous custom element.
    codehz
        3
    codehz  
       2020-09-22 16:25:02 +08:00
    上面有一点错误,一个元素是 Palpable content 不代表不是 flow content,应该说 div 不属于 flow content ... https://html.spec.whatwg.org/multipage/dom.html#kinds-of-content
    codehz
        4
    codehz  
       2020-09-22 16:36:09 +08:00
    不对,研究了一下标准,上面说的内容错了

    p 允许 Phrasing content
    div 不是 Phrasing content

    但是,由于自动闭合规则存在,所以<p><div></div>是合法的,多余的</p>才是不合法的,标准似乎没有规定多余的</p>的处理方案,有一个方法就是给他补一个<p>,于是变成<p><div></div><p></p>
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5379 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 07:30 · PVG 15:30 · LAX 00:30 · JFK 03:30
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.