Javascript:如何动态创建新的div,移动它,以各种方式修改它? [英] Javascript: How to create new div dynamically, change it, move it, modify it in every way possible?

查看:101
本文介绍了Javascript:如何动态创建新的div,移动它,以各种方式修改它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力这么几个小时,我会疯了。我想在页面加载时创建新的div。这些div将显示为根据JSON文件的外部数据更改的有序组。我需要一个for循环,因为需要超过100个div。



所以,我需要能够改变每个创建的div关于高度,宽度,上/左等。然而, document.getElementById(created_div)。style.whatever 什么都不做,我甚至不能看到一个新的div出现。我将新的divs height / width设置为500px,背景设置为red,依此类推,但是没有新的div显然出现。我究竟做错了什么?为什么这么难?啧。帮助!

解决方案



DOM操作的基础知识。请记住,新建节点在文档中是可见的,需要元素添加到正文或包含身体的节点。


I've been struggling with this for hours and I'm going insane. I want to create new divs as the page loads. These divs will appear as an ordered group which changes depending upon external data from a JSON file. I will need to do this with a for loop because there are over 100 divs needed.

So, I need to be able to change each created div in regards to height, width, top/left and so on. Yet, document.getElementById("created_div").style.whatever does nothing, I can't even see a single new div appear. I've set the new divs height/width to 500px, background to "red", and so on, but no new divs are definitely appearing. What am I doing wrong? Why is this so hard? Sheesh. Help!

解决方案

This covers the basics of DOM manipulation. Remember, element addition to the body or a body-contained node is required for the newly created node to be visible within the document.

这篇关于Javascript:如何动态创建新的div,移动它,以各种方式修改它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆