【发布时间】:2019-03-24 02:36:18
【问题描述】:
重置document.body 的outerHTML 属性有一个奇怪的副作用:它将额外的空<head></head> 添加到DOM 中,就在body 之前:
head { display: inline; counter-increment: h; border: 1px solid; }
head:last-of-type::after { content: 'Head elements count: ' counter(h); }
[onclick]::after { content: attr(onclick); }
<button onclick="document.body.outerHTML=document.body.outerHTML"></button>
所有浏览器似乎在这方面都是一致的。我已经been told 它被指定为这种方式,但无法挖掘权威标准的立场,甚至在讨论档案中也没有提及。你知道这方面的一些背景,还是有一些技术原因必须这样?有什么想法吗?
【问题讨论】:
-
因为 body 的内容是可替换的,但 body 本身不是?无论如何,它看起来像一个错误。
-
@ChaseChoi 但错误报告被关闭为无效引用它的行为符合规范。而且它似乎在浏览器中是通用的。
标签: javascript html dom outerhtml