我正在 html5rocks 上玩Shadow DOM 101 教程。我正在使用 Chrome 25.0.1364.172,当我尝试访问appendChild
Shadow DOM 根目录(如教程中所示)时,我得到一个
Uncaught Error: NotFoundError: DOM Exception 8
. 我想我错过了一些明显的东西,但我不知道是什么。这是代码:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Test the shadow dom</title>
</head>
<body>
<div id="myName">Alon</div>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script id="myNameTemplate" type="text/x-tmpl-template">
<style>
.outer {
border:2px solid brown;
border-radius: 1em;
background: red;
font-size: 28pt;
width: 12em;
height:2em;
text-align: center;
}
.boilerplate {
color:white;
font-family: sans-serif;
padding:0.5em;
}
.name {
color:black;
background: white;
font-family: "Marker Felt", cursive;
font-size: 45pt;
padding-top:0.2em;
}
</style>
<div class="outer">
<div class="boilerplate">
Hi! my name is
</div>
<div class="name">
alon
</div>
</div>
</script>
<script>
$(document).ready(function(){
var shadow = document.querySelector("#myName").webkitCreateShadowRoot();
console.log(shadow);// I get #shadow-root in the console
var template = $("#myNameTemplate").html();//also tried text(), without jQuery with innerHTML and other options
console.log(template);//I get the content of the template in the console
shadow.appendChild(template); //this part breaks
});
</script>
</body>
</html>
由于我的浏览器不支持<template>
教程中显示的新标签,因此我将其更改为<script type="text/x-tmpl">
.
编辑:当我尝试时,我从控制台收到同样的错误:
shadow.appendChild('<div></div>')
Error: NotFoundError: DOM Exception 8