我收到了一些其中包含屬性「title」的對象。動態創建div容器並將它們添加到現有的div
當正文加載時,我想創建一個新的div foreach對象存在。
當我這樣做時,div容器不會被創建,現在在控制檯中列出了錯誤。
重要提示:顯然,我的節點列表不是空的。我有一個測試例程,裏面有10個對象。所以,當我在createNodeContainer
功能
console.log(node.title);
寫寫下對象的標題正確。所以,我有「內容」,但也有加入到div「節點列表」
所以我當前的代碼看起來這不派息容器:
function loadNodes(){
var nodes = store.getNodes(); // get all the objects from a list
for (var i = 0; i < nodes.length; i++) {
createNodeContainer(nodes[i]); // pass in the current node of the nodeList
}
}
function createNodeContainer(node){ // create the new div
var newNodeContainer = document.createElement("div");
newNodeContainer.class = "nodeContainer"; // add some css
newNodeContainer.innerHTML = node.title; // write a text for testing it
$('nodeList').append(newNodeContainer); // add it to the parent div
}
和我的HTML是這樣的:
<body onLoad ="loadNodes()">
<div id="nodeList">
</div>
</body>
有人可以幫我嗎?
您需要添加# - $( '#節點列表') – Gerard
這是'.className'而不是'.class'。爲什麼混合使用jQuery和vanilla JS? [選擇器錯誤](https://api.jquery.com/category/selectors/)。 – Andreas
哦,該死的缺失#... – Question3r