zoukankan      html  css  js  c++  java
  • Js动态创建div

    这是原来的CSS样式
    .item{float:left;overflow:hidden;margin-left:8px;margin-top:10px; 320px; height:
    250px;background-repeat: no-repeat; background-image:url(../images/bgred.jpg)}
    .curve{position:relative;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'>&nbsp;关&nbsp;闭&nbsp;</span>"
     document.body.appendChild(oDiv);
     }
     else
     {
      hasdiv.innerHTML="<img src="+sid+"><br><span onclick=closediv() style='cursor:hand'>&nbsp;关&nbsp;闭&nbsp;</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;">

  • 相关阅读:
    ajaxFileUpload 实现多文件上传(源码)
    Springboot 热部署的两种方式
    基于树莓派3B+Python3.5的OpenCV3.4的配置教程
    Shiro 架构原理
    Cron表达式
    SpringBoot中Scheduled代码实现
    Linus安装mysql8
    查看虚拟机CENTOS7 的 IP 地址和命令
    linux vi保存退出命令 (如何退出vi)
    Linux常用命令大全
  • 原文地址:https://www.cnblogs.com/hateyoucode/p/1355728.html
Copyright © 2011-2022 走看看