Javascript中怎么动态创建DIV

Javascript中怎么动态创建DIV,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

Javascript动态创建DIV

这是原来的CSS样式

.item{float:left;overflow:hidden;margin-left:8px;  margin-top:10px;width:320px;height:250px;  background-repeat:no-repeat;  background-image:url(../images/bgred.jpg)}  .curve{position:relative;width:320px;height:250px;  z-index:1;left:75px;top:-40px;}

动态创建DIV代码如下:

for(j=0;j*8<str.length;j++)  {  varmyDIV=window.frames["displayFrame"]  .document.createElement("DIV");  myDIV.setAttribute("id","itemDIV"+j);  myDIV.style.styleFloat="left";  myDIV.style.overflow="hidden";  myDIV.style.marginLeft="8px";  myDIV.style.marginTop="10px";  myDIV.style.width="320px";  myDIV.style.height="250px";  myDIV.style.backgroundRepeat="no-repeat";  myDIV.style.backgroundImage="url(image/bgred.jpg)" window.frames["displayFrame"].document  .body.appendChild(myDIV);  varcurveDIV=window.frames["displayFrame"]  .document.createElement("DIV");  curveDIV.setAttribute("id","curveDIV"+j);  curveDIV.style.position="relative";  curveDIV.style.zIndex=1;  curveDIV.style.left="75px";  curveDIV.style.top="-40px";  curveDIV.style.width="320px";  curveDIV.style.height="250px";  window.frames["displayFrame"].document  .getElementById("DIVitem"+j).appendChild(curveDIV);  }

把DIV元素增加到HTML里面。

也可在HTML里面定义一个SPAN

window.frames["displayFrame"].document  .getElementById("spanId").appendChild(myDIV);  window.frames["displayFrame"].document  .body.appendChild(myDIV);

IE和Firefox都支持.

◆另外需要注意的是这个CSS元素

浮动效果:float:left
在IE下代码为:myDIV.style.styleFloat="left";
在Firefox代码为:myDIV.style.cssFloat="left";

其他的诸如这种元素:

在CSS编写中一般是:margin-left:8px,而在动态增加需要去掉-:myDIV.style.marginLeft="8px"。

看完上述内容,你们掌握Javascript中怎么动态创建DIV的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注行业资讯频道,感谢各位的阅读!