Javascript 无法将元素附加到div
我的Javascript旨在执行以下操作 我有一个具有6个子节点的父div。我用JS创建了3个div,然后将它们附加到父对象中。然后,我将原来的6个子节点插入刚才创建并附加到原始父节点的3个div中。这3个div是最初6个元素的兄弟,现在是它们的父母 这几乎奏效了。但两个原始子元素并没有像我在Javascript中所希望的那样附加到中间div。我不知道我做错了什么。下面是Javascript,这里是指向正在执行脚本的网页的链接Javascript 无法将元素附加到div,javascript,html,append,Javascript,Html,Append,我的Javascript旨在执行以下操作 我有一个具有6个子节点的父div。我用JS创建了3个div,然后将它们附加到父对象中。然后,我将原来的6个子节点插入刚才创建并附加到原始父节点的3个div中。这3个div是最初6个元素的兄弟,现在是它们的父母 这几乎奏效了。但两个原始子元素并没有像我在Javascript中所希望的那样附加到中间div。我不知道我做错了什么。下面是Javascript,这里是指向正在执行脚本的网页的链接 编辑:复制错误使用您更新的问题,我已尝试。正如我所说,我避开了你的
编辑:复制错误使用您更新的问题,我已尝试。正如我所说,我避开了你的方法,这有点过头了。您在JSFIDLE中启用了jQuery,所以我使用了它 正如你提到的,你正在学习,我已经包括了一些应该解释发生了什么的评论
// Set this jQuery to run once the page is ready.
$('document').ready(function() {
// Create a list of all the headers & all the contents
var headers = $('.fc-header');
var contents = $('.fc-content');
// Cycle through the headers & contents (assuming they're equal in length)
for(i=0; i<headers.length; i++) {
// Add a div to calendar with an id of the for iterator so we can
// access it.
$('#calendar').append('<div id="' + i + '"></div>');
// Access that div using the id we added, then append the i'th header
// and the i'th content.
$('#' + i).append(headers[i]);
$('#' + i).append(contents[i]);
// Remove the id as we don't need it later.
$('#' + i).removeAttr('id');
}
})
//将此jQuery设置为在页面准备就绪后运行。
$('document').ready(函数(){
//创建所有标题和所有内容的列表
var headers=$('.fc header');
var contents=$('.fc content');
//循环浏览标题和内容(假设它们的长度相等)
对于(i=0;i如果可以的话,您应该尝试将问题复制为JSFIDLE,这将使调试变得非常容易,而不需要访问您的开发环境。此外,懒惰的程序员将创建可重用函数,可能还会创建类似于此的jQuery。您这样做完全没有必要。啊,好的。谢谢.我试试看。我很感激你的回答。你说得对。我不懒惰。只是很不熟练。哈!
// Set this jQuery to run once the page is ready.
$('document').ready(function() {
// Create a list of all the headers & all the contents
var headers = $('.fc-header');
var contents = $('.fc-content');
// Cycle through the headers & contents (assuming they're equal in length)
for(i=0; i<headers.length; i++) {
// Add a div to calendar with an id of the for iterator so we can
// access it.
$('#calendar').append('<div id="' + i + '"></div>');
// Access that div using the id we added, then append the i'th header
// and the i'th content.
$('#' + i).append(headers[i]);
$('#' + i).append(contents[i]);
// Remove the id as we don't need it later.
$('#' + i).removeAttr('id');
}
})