• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

java web 动态添加“div” for循环自动添加、自动换行

武飞扬头像
阔野星火
帮助2

        曾经在学习Java的时候,做过一个电商方面的程序设计,其中一个需求就是根据数据库中商品的数量来动态的添加div,同时展示商品图片、名称、价格等信息,就和某些网购平台展示商品的页面差不多。其他功能不谈,下面主要讲的是动态的添加div。

        实现的思路是,先在body标签中建一个空白的div,给它加上id当作外壳,然后将div通过字符串拼接的方式拼接起来,在通过for循环与append()方法向事先创建好的外壳div中添加。过程需要用到 jQuery。

普通排放

如果只是单单的实现自动添加div,那是非常简单的,两步就能搞定。

一、创建外壳div,id设为shell

  1.  
    <body>
  2.  
     
  3.  
    <div id="shell"> </div>
  4.  
     
  5.  
    </body>

二、创建方法,字符串拼接字符串,我这里给div设置了一些样式,方便展示的时候查看。for循环的条件可以根据自己需要来改变。如果和后端结合,则也可根据后端传来的条件进行循环。

  1.  
    <script type="text/javascript">
  2.  
    $(function() {
  3.  
     
  4.  
    a = "<div style='width: 200px; height: 200px; background-color: blue; font-size:30px; margin-top: 200px; margin-left: 200px;'></div>";
  5.  
     
  6.  
     
  7.  
    for (var n = 0; n < 16; n ) {
  8.  
     
  9.  
    $("#shell").append(a);
  10.  
     
  11.  
    }
  12.  
     
  13.  
    });
  14.  
     
  15.  
    </script>
学新通

实现结果如下

                        学新通

 ok,搞定。

横向排放

你以为这样就结束了吗?我们还可以通过添加浮动来实现div的横向排放。

还是和上面的操作一样,这次只不过是在样式中加了一个浮动float: left

  1.  
    <script type="text/javascript">
  2.  
    $(function() {
  3.  
     
  4.  
    a = "<div style='width: 200px; height: 200px; background-color: blue; font-size:30px; margin-top: 200px; margin-left: 200px; float: left;'></div>";
  5.  
     
  6.  
     
  7.  
    for (var n = 0; n < 16; n ) {
  8.  
     
  9.  
    $("#shell").append(a);
  10.  
     
  11.  
    }
  12.  
     
  13.  
    });
  14.  
     
  15.  
    </script>
学新通

实现效果如下,这种方法进行添加的div都是在一行中显示,如果页面的比例发生变换,那每行的div个数就会发生变化

学新通

改变网页比例后

学新通

换行操作

通过if...else...来进行换行判断,我这里是设置了一行四个div

同时我这里多添加了一个字符串b,也是div的拼接,当满足条件时就添加b。

这里我给b先设置了一个清除浮动,之后再添加一个浮动,这样做的目的是切断与a的浮动联系以实现换行,再次添加浮动则是在换行之后能继续与a进行相连。

(我还偷偷加了一张图片和一些文字,因为我觉得之前的结果太单调了)

  1.  
    <script type="text/javascript">
  2.  
    $(function() {
  3.  
     
  4.  
    a = "<div style='width: 200px; height: 200px; background-color: blue; font-size: 30px; margin-top: 200px; margin-left: 200px; float: left;'>"
  5.  
    "<img src='https://blog.csdn.net/weixin_45891573/article/details/img/myimg.jpg' style='width:200px; height:200px ' />" "&nbsp;&nbsp;&nbsp;&nbsp;小恐龙</div>";
  6.  
     
  7.  
     
  8.  
    b ="<div style='width: 200px; height: 200px; background-color: gold; margin-top: 200px; clear: both; float: left;'></div>";
  9.  
     
  10.  
    for (var n = 0; n < 16; n ) {
  11.  
     
  12.  
     
  13.  
    if (n % 4 != 0) {
  14.  
     
  15.  
    $("#shell").append(a);
  16.  
    } else {
  17.  
    $("#shell").append(b);
  18.  
    }
  19.  
     
  20.  
    }
  21.  
     
  22.  
     
  23.  
     
  24.  
    });
  25.  
     
  26.  
    </script>
学新通

实现结果如下,其中金黄色的部分就是b中结果,这样子不管怎么改变网页的比例,每行的div个数都会固定不变。

 学新通

完整代码如下

  1.  
    <!DOCTYPE html>
  2.  
    <html>
  3.  
    <head>
  4.  
    <meta charset="utf-8">
  5.  
    <title></title>
  6.  
    <script src="js/jquery-3.6.0.js"></script>
  7.  
    </head>
  8.  
    <script type="text/javascript">
  9.  
    $(function() {
  10.  
     
  11.  
    a = "<div style='width: 200px; height: 200px; background-color: blue; font-size: 30px; margin-top: 200px; margin-left: 200px; float: left;'>"
  12.  
    "<img src='https://blog.csdn.net/weixin_45891573/article/details/img/myimg.jpg' style='width:200px; height:200px ' />" "&nbsp;&nbsp;&nbsp;&nbsp;小恐龙</div>";
  13.  
     
  14.  
     
  15.  
     
  16.  
    b ="<div style='width: 200px; height: 200px; background-color: gold; margin-top: 200px; clear: both; float: left;'></div>";
  17.  
     
  18.  
     
  19.  
     
  20.  
    for (var n = 0; n < 16; n ) {
  21.  
     
  22.  
     
  23.  
    if (n % 4 != 0) {
  24.  
     
  25.  
    $("#shell").append(a);
  26.  
    } else {
  27.  
    $("#shell").append(b);
  28.  
    }
  29.  
     
  30.  
    }
  31.  
     
  32.  
     
  33.  
    });
  34.  
    </script>
  35.  
     
  36.  
    <body>
  37.  
     
  38.  
    <div id="shell"> </div>
  39.  
     
  40.  
    </body>
  41.  
    </html>
学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhiajehi
系列文章
更多 icon
同类精品
更多 icon
继续加载