Js动态创建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++) 
{ 
var mydiv = 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); 
var curvediv =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"; 
关于大小写敏感问题没有仔细研究. 

 

 

 

<script type="text/javascript" defer>
function cDiv(){
var oDiv=document.createElement("div");
oDiv.style.border="1px solid black";
oDiv.style.width="100px";
oDiv.style.height="100px";
oDiv.style.backgroundColor="#E1E1E1";
document.body.appendChild(oDiv);
}
oF.document.write("<script type=\"text/javascript\">");
oF.document.write("function clk(){");
oF.document.write("window.top.document.getElementsByTagName(\'button\')[0].click();");
oF.document.write("}");
oF.document.write("<\/sc"+"ript>");
oF.document.write("<button onclick=\"clk()\">create Div in parent<\/button>");

</script>
<body>
<iframe id="oF" frameborder="1">

</iframe>
<button id="btn" onclick="cDiv()">create</button>
</body>
 


 

 

 

 

<script>
function show(sid)
{
 var hasdiv=document.getElementById("showpic")
 if (hasdiv==null)
 {
 var oDiv=document.createElement("div");
 oDiv.id="showpic";
 oDiv.style.border="1px solid black";
 oDiv.style.marginLeft="300px";
 oDiv.style.marginTop="-500px";
 oDiv.style.position='absolute';
 oDiv.style.fontSize='10pt';
 oDiv.style.color='red';
 oDiv.style.lineHeight='25px';
 oDiv.style.textAlign='right';
 oDiv.innerHTML="<img src="+sid+"><br><span onclick=closediv() style='cursor:hand'> 关 闭 </span>"
 document.body.appendChild(oDiv);
 }
 else
 {
  hasdiv.innerHTML="<img src="+sid+"><br><span onclick=closediv() style='cursor:hand'> 关 闭 </span>"
 }
}

function closediv()
{
 var hasdiv=document.getElementById("showpic")
 hasdiv.innerHTML=""
}
</script> 

 

<img src="<%=rs_Product("CompHonor")%>" width="149" height="113" border="0" onclick="show('<%=rs_Product("CompHonor")%>')" style="cursor:hand;">

posted on 2011-05-31 17:59  dawnruby  阅读(495)  评论(0)    收藏  举报

导航