我有一个自定义元素,我想用DOM模板标记填充它。除了html和body标记之外,所有东西似乎都被插入到自定义元素中。这是否意味着在自定义元素中不允许使用html和body标记?这是我的密码
<template id="legacy-code-template">
<style>
body {
background:red;
}
</style>
<html><body><p>Sample text</p></body></html>
</template>
<legacy-code></legacy-code>
<script>
class LegacyCode extends HTMLElement {
constructor() {
super();
const template = document
.getElementById('legacy-code-template')
.content;
const shadowRoot = this.attachShadow({mode: 'open'})
.appendChild(template.cloneNode(true));
}
}
customElements.define('legacy-code', LegacyCode);
</script>发布于 2018-10-06 15:09:43
是。
HTML5文档的根节点必须是<html>元素。<html>元素的子节点必须是<head>和<body>元素。
实际上是你可以省略它们,但它们是隐含的。
在您的文档中,如果不指定<html>、<head>和<body>元素,浏览器将添加它们,并且您定义的根节点将根据它们的类型和它们在页面中的位置被添加到<head>或<body>中:
<link>,<meta>,<script>.如果它们位于文档的开头,则在<head>内部。<div>、<span>、<section>和<body>中的自定义元素。自定义元素的内容模型类型为流、短语化或可缩合 content。这意味着它必须插入到<body>元素中。
自定义元素中的<html>和<body>无效并被忽略。
https://stackoverflow.com/questions/52674019
复制相似问题