【发布时间】:2015-06-02 14:57:37
【问题描述】:
我尝试使用来自 https://github.com/webcomponents/webcomponentsjs 的 polyfill 制作自己的 Web 组件
这是我的代码:
im-list.html
<template id="im-list-temp">
<style>
:host {
list-style-type: none;
margin: 0;
padding: 0;
}
</style>
<content> </content>
</template>
<script>
var currentScript = document._currentScript || document.currentScript;
var proto = Object.create(HTMLUListElement.prototype, {
createdCallback: {
value: function() {
var doc = currentScript.ownerDocument;
var t = doc.querySelector("#im-list-temp");
var clone = doc.importNode(t.content, true);
var root = this.createShadowRoot();
root.appendChild(clone);
}
}
});
document.registerElement('im-list', {
prototype: proto,
extends: 'ul'
});
</script>
index.html
<!DOCTYPE html>
<html>
<head>
<script src="bower_components/webcomponentsjs/webcomponents.js"></script>
<link rel="import" href="./components/im-list.html" />
<title>List Test</title>
</head>
<body>
<ul is="im-list">
<li>Blubb</li>
<li>Blubb Blubb</li>
</ul>
</body>
</html>
此代码在 Chrome (43.0.2357.81) 中运行良好,但在 Firefox(38.0.1 和 40.0a2)和 Safari(8.0.6)中无法运行。在 FF 和 Safari 中,<style> 只是添加到普通 DOM 中。
【问题讨论】:
标签: javascript html css web-component shadow-dom