我想使用HTML 5模板标记,但是在页面刷新之后有一些令人困惑的行为。也许我先给你看一些代码:
<!doctype html>
<meta charset=utf-8>
<title>HTML 5 - template test</title>
<template>
<h1>This is an awesome new HTML 5 template</h1>
<p>.. some even more awesome content .. some even more awesome content .. some even more awesome content .. some even more awesome content .. some even more awesome content</p>
</template>
<div></div>
<script>
var template = document.querySelector('template'),
templateContent = template.content.cloneNode(true);
console.log(template);
console.log(templateContent);
document.querySelector('div').appendChild(templateContent);
</script>实际上,这和预期的一样,但是控制台中有一些令人困惑的事情发生。当我第一次打开页面时,上面写着:

但是当我刷新页面时,它会显示如下:

当我重复一遍的时候,我又看到了第一张照片上的东西。我也在jsbin上尝试过,但是我看不到这种行为:http://jsbin.com/ofotah/1
我真的不知道那里发生了什么事。我正在使用谷歌Chrome版27.0.1453.110 (Windows)。希望你能帮我这个忙。
发布于 2013-06-07 20:13:25
这并不是真正的模板相关,它是一个古怪的Chrome怪癖,有时会表现出来。在几乎不可能再现的情况下,它也会对其他元素标记执行同样的操作,但是实际的对象内容不会受到影响。它有时捕获toString()序列化,有时获取对象序列化。
然而,正如Andbdrew所指出的,模板标记仍然是一个草案元素,我们还远未决定如何正确地使用它或如何访问它们,因此,考虑到示例代码如何使用它,我建议只使用<script type="text/template">...</script>元素,并使用document.querySelector("script[type='text/template']");来选择它,而不是使用实验性的元素=)
https://stackoverflow.com/questions/16990169
复制相似问题