0

我想多次复制整个 div(即;在 A4 纸大小的左上角、右上角、左下角、右下角)

    <script language="javascript" type='text/javascript'>
    function updateDivs() {
     var repeat_container = document.getElementById("layout");
     var div_element;

    for (var i = 0; i < 4; i++) {
        div_element = document.createElement("div");
        repeat_container.appendChild(div_element);
      }
    }
   </script>


  <div class = "layout">
 <div class="header"><asp:label id="label1" runat="server/></div>
   <div class="chart"><asp:label id="label2" runat="server/></div>
    <div class="legend"><asp:label id="label3" runat="server/></div>
     <div class="info1"><asp:label id="label4" runat="server/></div>
      <div class="info2"><asp:label id="label5" runat="server/></div>
      <div class="info3"><asp:label id="label6" runat="server/></div>............
  </div>

  ![enter image description here][1]
  the screen shot is attached below
4

1 回答 1

0

你不能用 javascript 来做,因为你有 asp.net 控件。为您的每个 div 添加runat="server"和更改他们的ID to Upperleftcorner, Upperrightcornerbottom, bottomleftcorner, bottomrightcorner.

代码:

          List<HtmlGenericControl> alldivs=new List<HtmlGenericControl>(4);
            alldivs.Add(Upperleftcorner);
            alldivs.Add(Upperrightcornerbottom);
            alldivs.Add(bottomleftcorner);
            alldivs.Add(bottomrightcorner); 
            foreach(HtmlGenericControl divparent in alldivs)
            {
            HtmlGenericControl divholder= new HtmlGenericControl("div");
            divholder.Attributes.Add("class", "layout");
            for(int i=1;i<=6;i++)
            {
            HtmlGenericControl div= new HtmlGenericControl("div");
            if(i==1)
            div.Attributes.Add("class", "header");
            if(i==2)
            div.Attributes.Add("class", "chart");
            if(i==3)
            div.Attributes.Add("class", "legend");
            if(i==4)
            div.Attributes.Add("class", "info1");
            if(i==5)
            div.Attributes.Add("class", "info2");
            if(i==6)
            div.Attributes.Add("class", "info3");
            Label l=new Label();
            div.Controls.Add(l);
            divholder.Controls.Add(div);
            }
            divparent.Controls.Add(divholder);
            }

使用 javascript,您将不得不使用<span>

<script language="javascript" type='text/javascript'>
    function updateDivs() {
     var repeat_container = document.getElementById("layout");
     var div_element='<div class = "layout"><div class="header"><span  class="label1" /></div><div class="chart"><span  class="label2" /></div><div class="legend"><span  class="label3" /></div><div class="info1"><span  class="label4" r/></div><div class="info2"><span  class="label5" /></div><div class="info3"><span class="label6" /></div></div>';

    for (var i = 0; i < 4; i++) {
        div_element = document.createElement("div");
        repeat_container.appendChild(div_element);
      }
    }
   </script>
于 2012-07-02T06:51:42.140 回答