快跑蜗牛哥 2018-02-07 06:32 采纳率: 0%
浏览 1036
已采纳

在js中控制样式出现问题,求助。

在html页面需要显示以下数据

 <!--第一行数据-->
<div class="main_store_slide" style="margin-top:70px;">
  <div class="swiper-container swiper-container-horizontal swiper-container-free-mode">
    <div class="swiper-wrapper">
      <!--1-->
      <div class="swiper-slide swiper-slide-active lawyer_b" >
         <ul class="ul1" style="display: block;">
           <li class="s1">122</li> 
           <li class="s2">2323</li>  
        </ul> 
      </div>
      <!--2-->
     <div class="swiper-slide swiper-slide-next lawyer_b" >
        <ul class="ul1" style="display: block;">
           <li class="s1">3434</li> 
           <li class="s2">454</li>
        </ul> 
     </div>
     <!--3-->
     <div class="swiper-slide swiper-slide-next lawyer_b" >
        <ul class="ul1" style="display: block;">
           <li class="s1">334</li> 
           <li class="s2">343443</li>
        </ul> 
     </div>
     <!--4-->
     <div class="swiper-slide swiper-slide-next lawyer_b" >
        <ul class="ul1" style="display: block;">
           <li class="s1">12</li> 
           <li class="s2">34</li>
        </ul> 
     </div>
  </div>
</div>
</div>
<!--end第一行数据-->

<!--第二行数据-->
<div class="main_store_slide" style="margin-top:70px;">
  <div class="swiper-container swiper-container-horizontal swiper-container-free-mode">
    <div class="swiper-wrapper">
       <!--1-->
      <div class="swiper-slide swiper-slide-active lawyer_b" >
         <ul class="ul1" style="display: block;">
           <li class="s1">122</li> 
           <li class="s2">2323</li>  
        </ul> 
      </div>
      <!--2-->
     <div class="swiper-slide swiper-slide-next lawyer_b" >
        <ul class="ul1" style="display: block;">
           <li class="s1">3434</li> 
           <li class="s2">454</li>
        </ul> 
     </div>
     <!--3-->
     <div class="swiper-slide swiper-slide-next lawyer_b" >
        <ul class="ul1" style="display: block;">
           <li class="s1">334</li> 
           <li class="s2">343443</li>
        </ul> 
     </div>
     <!--4-->
     <div class="swiper-slide swiper-slide-next lawyer_b" >
        <ul class="ul1" style="display: block;">
           <li class="s1">12</li> 
           <li class="s2">34</li>
        </ul> 
     </div>
  </div>
</div>
</div>
<!--end第二行数据-->
……

因这些数据是从接口中读取,然后在页面上显示,
所以,在js中控制相关样式,如下所示:

 <script type="text/javascript">
<!--
    $(function(){

       var str=""
       var j=0;
       for(var i=0;i<18;i++){
             j++
            if(j%4==0){
               str+="<div class='main_store_slide' style='margin-top:70px;'>";
                 str+="<div class='swiper-container swiper-container-horizontal swiper-container-free-mode'>";
                 str+="<div class='swiper-wrapper'>";
             }
              str+="<div class=\"swiper-slide swiper-slide-active lawyer_b\" >";
              str+="<ul class=\"ul1\" style=\"display: block;\">";
                  str+="<li class=\"s1\">12</li>";
              str+="<li class=\"s2\">34</li>";  
                   str+=" </ul> </div>";
          if(j%4==0){ 
                 str+="</div>";
                 str+="</div>";
                 str+="</div>";
                  }

       }
 $("#divid").html(str);
    })

//-->
</script>

请问 以上 js中该 如何修改。
有试过 用 :nth-of-type ,发现 样式乱了。

  • 写回答

5条回答 默认 最新

  • 斯洛文尼亚旅游 2018-02-07 06:51
    关注

    你结构都错了,加nth-of-type有什么用,用2层for不是好理解点

       $(function () {
            var str = ""
            for (var i = 0; i < 18; i += 4) {
    
                str += "<div class='main_store_slide' style='margin-top:70px;'>";
                str += "<div class='swiper-container swiper-container-horizontal swiper-container-free-mode'>";
                str += "<div class='swiper-wrapper'>";
                for (var j = i, k = i + 4; j < k; j++) {
                    str += "<div class=\"swiper-slide swiper-slide-active lawyer_b\" >";
                    str += "<ul class=\"ul1\" style=\"display: block;\">";
                    str += "<li class=\"s1\">" + i + '-' + j + "</li>";
                    str += "<li class=\"s2\">34</li>";
                    str += " </ul> </div>";
                }
    
                str += "</div>";
                str += "</div>";
                str += "</div>";
    
            }
            $("#divid").html(str);
        });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。
  • ¥30 c#打开word开启修订并实时显示批注
  • ¥15 如何解决ldsc的这条报错/index error
  • ¥15 VS2022+WDK驱动开发环境
  • ¥30 关于#java#的问题,请各位专家解答!
  • ¥30 vue+element根据数据循环生成多个table,如何实现最后一列 平均分合并
  • ¥20 pcf8563时钟芯片不启振
  • ¥20 pip2.40更新pip2.43时报错
  • ¥15 换yum源但仍然用不了httpd
  • ¥50 C# 使用DEVMOD设置打印机首选项