根据id在另一个元素中创建元素



rigth现在我有了这个代码来绘制一些div(基于一些逻辑,我将有父级和子级。

array.forEach(async (a, b) => { 
var currentDiv = 0;
let divHtml;
if (b === 0) {
//This should be the first parent 
divHtml = document.createElement('div');
divHtml.id = 'someID' + currentDiv;
} else if (previousDiv != currentDiv) {
// This should be used for next parents based on the previousDiv & currentDiv logic
divHtml = document.createElement('div');
divHtml.id = 'someID' + currentDiv;
} else {
// This should be used only for childs. I want to create another div but inside the parent that I have stored into currentDiv.
divHtml = document.getElementById('someID' + currentDiv);
divHtml.id = 'someChildID' + currentDiv;
}
// Some more code below but what it's important is this (I continue using the divHtml in a lot of places on my code):
divHtml.setAttribute('someData', someDataAttribute);
}); 

所以,我的问题是:如果有一种方法可以获得parentDiv并在X子元素中绘制,我该怎么做?我试过这个:

divHtml = document.createElement('div').appendTo($('#someID' + currentDiv));

但是我得到了.appendTo((不是一个函数错误消息。

.appendTo()是一个jQuery方法,看起来你使用的是纯javascript,也许你想使用appendappendChild

最新更新