我现在正在努力将一组div插入到聚合物中的纸影内的核心标题面板中。我将从一个JSON数组中获取一组值,并且需要为每个值创建一个包含HTML内容(最终是复选框)的div。下面是相关的代码片段:
<paper-shadow class="card upload-options-box" z="1">
<core-header-panel flex id="graphHeaderList">
<core-toolbar class="upload-option-header">
<span flex>Variable</span>
</core-toolbar>
<core-label horizontal layout> // this is all just an example of what I eventually want to insert
<paper-checkbox for></paper-checkbox>
<div vertical layout>
<h4>MakeCircuitCreated</h4>
</div>
</core-label> // end of example of insert
</core-header-panel>
</paper-shadow>和JQuery:
function addHeaders(){
$.getJSON('http://myURL/getDataHeaders', function(data) {
var headerArray = [];
$.each(data, function(key, val) {
$('#graphHeaderList').append("<div id='" +val +"'></div>");
console.log(val);
headerArray.push(val)
});
console.log(headerArray);
});
}
window.onload = addHeaders; //grabs the header values when the page is loaded我的JSON:
{
"headers": [
"MakeSpawnFish",
"MakeEndGame",
"MakeModeChange",
"MakeConnectComponent",
"MakeCircuitCreated",
"MakeStartGame",
"MakeSnapshot",
"MakeResetBoard",
"MakeAddComponent",
"MakeCaptureFish",
"MakeRemoveComponent",
"MakeDisconnectComponent"
]
}发布于 2015-01-11 09:09:00
在我看来,你的$.each调用了错误的东西。
在您的json文件中有一个名为headers的密钥。当您的each函数迭代时,它将获得1个键,并将其成员(一个数组)添加到div。我对它进行了测试,每个成员都有一个div作为它的id!
因此,您可能需要在内部数组上嵌套第二个each函数
$.each(data, function(key, val) {
$.each(val, function(index,value){
$('#graphHeaderList').append("<div id=" + "'" + value + "'" + "></div>");
console.log(value);
headerArray.push(value);
}
);发布于 2015-05-07 00:20:24
你的代码:
$('#graphHeaderList').append("<div id='" +val +"'></div>");不起作用,因为jQuery在其作用域中找不到该元素。但是,聚合物通过其automatic node finding功能自动创建id元素引用:
this.$.graphHeaderList因此,如果您使用以下命令,它将按预期工作:
$(this.$.graphHeaderList).append("<div id='" +val +"'></div>");https://stackoverflow.com/questions/27882877
复制相似问题