我正在玩 Google 的 Polymer-Project Web-Components Library。我想构建一个包含画布元素的自定义标签,并通过 javascript 从自定义元素中访问它。但我无法使用 document.querySelector 或 document.getElementById 访问它 - 任何类似问题的提示或经验都值得赞赏。
这是我的来源:
<polymer-element name="sprite-canvas">
<template>
<style>
.leinwand {
width:200px;
height:200px;
background-color: lightblue;
}
</style>
<canvas class="leinwand" width="200" height="200"></canvas>
</template>
<script>
Polymer('sprite-canvas', {
leinwand : null,
ready: function() {
console.log(document.getElementsByTagName('canvas'));
console.log(document.querySelector('.leinwand'));
},
domReady: function() {
console.log(document.getElementsByTagName('canvas'));
console.log(document.querySelector('.leinwand'));
},
detached: function() { },
});
</script>
console.log 输出总是返回一个 NULL / 空集合,不管我在生命周期的哪个部分尝试调用画布。我的错误在哪里,我做错了什么?
最好的问候,卢波