【发布时间】:2016-03-17 09:50:54
【问题描述】:
鉴于此<template>:
<template id="my-template">
<h1 id="h">Hello!</h1>
</template>
还有 JS:
var t = document.querySelector("#my-template");
var h = t.content.querySelector("h1");
h.outerHTML = "<h3>AAAAAAAAAAAAAA</h3>";
有趣的是它可以在 FireFox 和 Edge 中运行,但在 Chrome 中 outerHTML 需要父元素,否则会在控制台中抛出错误 (https://chromium.googlesource.com/chromium/blink/+/master/Source/core/dom/Element.cpp#2528):
<template id="my-template">
<div>
<h1 id="h">Hello!</h1>
</div>
</template>
见https://jsfiddle.net/q5fmn186/11/
我的问题是,Chrome 的行为是否正确?是否应该在 <template> 中设置 outerHTML 不适用于直接子代?为什么其他网络浏览器不将其视为错误?
【问题讨论】:
标签: html dom web-component html5-template