How to repeat div using jQuery or JavaScript?

2019-07-29 22:59发布

I have an HTML page with the following div tags

<div data-role="page" id="pageHome" data-theme="a">
  <div data-role="content">
    <center>
      <h3><a href="#pageHome" data-role="none"><img src="images/logo.png" /></a></h3>
    </center>
    <div class="dydivarr0"> </div>
    <br />
    <div data-role="controlgroup" class="ui-btn-right"> <a href="#page1" data-role="button">Next</a> </div>
  </div>
</div>



<div data-role="page" id="page1" data-theme="a">
  <div data-role="content">
    <center>
      <h3><a href="#pageHome" data-role="none"><img src="images/logo.png" /></a></h3>
    </center>
    <div class="dydivarr1"> </div>
    <br />
    <div data-role="controlgroup" class="ui-btn-right"> <a href="#page2" data-role="button">Next</a> </div>
  </div>
</div>

<div data-role="page" id="page2" data-theme="a">
  <div data-role="content">
    <center>
      <h3><a href="#pageHome" data-role="none"><img src="images/logo.png" /></a></h3>
    </center>
    <div class="dydivarr2"> </div>
    <br />
    <div data-role="controlgroup" class="ui-btn-right"> <a href="#page3" data-role="button">Next</a> </div>
  </div>    

dydivarr is generated dynamically from web a response using JSON. Using that response I create dynamic div content, next I want to repeat that div as shown above. Is there any better way than to repeat static html code? I guess there must be few jQuery or JavaScript tricks for that.

Any help would be appericiated.

4条回答
放荡不羁爱自由
2楼-- · 2019-07-29 23:23

Something like this? Sample Fiddle

var lastSection = $('[data-role="page"]:last'); //get the last page probably you can use this to increment the id based on last id
var newSection = lastSection.clone(false);//default is false anyways, if you want to carry over data and events you can use true.
newSection.attr('id','setthenewid');//set the new id
$('div[class^=dydivarr]',newSection).attr('class','newclass'); //set the new class for your divarr
$('.ui-btn-right a',newSection).attr('href','newurlmap');//set the new url map 
lastSection.after(newSection); //place the entire page section in the end
查看更多
劳资没心,怎么记你
3楼-- · 2019-07-29 23:24

So you want cloneNode of your div, so try something like this

var div = document.getElementById('myDiv'),
clone = div.cloneNode(true);
clone.id = "some_id";
document.body.appendChild(clone);
查看更多
混吃等死
4楼-- · 2019-07-29 23:27
乱世女痞
5楼-- · 2019-07-29 23:33

Update :

Simple Code worked. It was just my wrong approach

<script type="text/javascript">

for(var lp=0; lp < 10; lp++)
{
document.write('<div data-role="page" id="page'+lp+'" data-theme="a">');
document.write('  <div data-role="content">');
document.write('    <center>');
document.write('      <h3><a href="#page0" data-role="none"><img src="images/logo.png" /></a></h3>');
document.write('     </center>');
document.write('     <div class="dydivarr+lp+'"> </div>');
document.write('     <br /> ');
document.write('     <div data-role="controlgroup" class="ui-btn-left"> <a href="#page'+(lp-1)+'"     data-role="button">Previous</a> </div>');
document.write('     <div data-role="controlgroup" class="ui-btn-right"> <a href="#page'+(lp+1)+'" data-role="button">Next</a> </div>');
document.write('   </div>');
document.write(' </div>');

}

</script>

Thanks

查看更多
登录 后发表回答