我是Polymer的新手 .
我想实现一个容器,我将在其中以编程方式在我的应用程序中添加其他元素 . 但我不能这样做 .
这是我的自定义组件(ay-box.html):
<dom-module id="ay-box">
<template>
<div id="container" style="display: inline-block;">
<div id="content" class="content">
<content></content>
</div>
<div id="footer" class="footer">
<label># of Items : </label>
<label>{{item_count}}</label>
</div>
</div>
</template>
<script>
Polymer({
is: "ay-box",
properties: {
item_count: {
type: Number,
value: 0
},
}
});
</script>
</dom-module>
在我的index.html中
<html>
<head>
....
<script type="text/javascript">
function loadImg(){
var mainBox = document.querySelector("#mainbox");
for(var i = 0; i< 10;++i){
var img = document.createElement("img");
img.id = "img" + i;
img.src = "img.png";
img.draggable = true;
mainBox.appendChild(img);
}
}
</script>
</head>
<body>
<ay-box id=mainbox></ay-box>
<button onclick="loadImg()">click</button>
</body>
</html>
当我点击按钮时,我正在等待在标签的位置看到图像 . 但是,它会将图像直接附加到标记下方 . 我不应该使用像这个例子中的直接javascript dom修改 . 我的错是什么?
1 回答
在Polymer 1.0中,您应该使用DOM API来追加子项 .
Use :
Polymer.dom(mainbox).appendChild(img);
Instead of :
mainBox.appendChild(img);
查看下面的工作示例 .