修复切片瓷砖javascript

时间:2017-06-08 08:36:00

标签: title cut

我有代码切割瓷砖: TITL1 - TITLE2 [XXX]

   var Enumber1 = new Array();
           $("#id").each(function(i){
           var text = $(this).text();
           if(text.indexOf('[') != -1 || text.indexOf(']') != -1){
             var Ntext1      = text.split('[')[0];
             Enumber1[i] = text.split('[')[1].split(']')[0];
             $(this).text(Ntext1);
           }
         });
         $("#id").each(function(i){         
           $(this).fadeIn("slow");
           if(Enumber1[i] != undefined){
             $(this).text(Enumber1[i]);
           }else{
             $(this).css('N/A');
           }


    });
  • TITLE1 -TITL2>>> div class =" title"
  • [XXX]>>> div class =" cut" 演示:https://jsfiddle.net/bro69zvb/

帮我解决问题

  • TILE1 in>>>在
  • 2.TITLE>>>在
  • XXX>>>在

感谢!!!

1 个答案:

答案 0 :(得分:0)

在您的脚本区域中使用它:

var Enumber1 = new Array();
var longOfArray=0;
$(".title").each(function(i){
  var text = $(this).text();  
  var res = text.replace(" -","");
  res =  res.split(" ");
  //res contains "title1,title2,[xxx]"  
    longOfArray = res.length;
  //lenght of this array is 3
  Enumber1 = res;
  //stored into your array
  $(".title").html("");
  //emptied first title
  });


  $(".cut").each(function(i){   
  //iterate through array to fill .cut div
  for(i=0;i<longOfArray; i++){

    if(Enumber1[i].indexOf("[")!=-1){            
  //remove first lash [
             var firstLash = Enumber1[i].indexOf("[");     
       Enumber1[i] = Enumber1[i].substring(firstLash+1);
    } 
    if(Enumber1[i].indexOf("]")!=-1){  
     //remove last lash ]
       var lastLash = Enumber1[i].indexOf("]");
       Enumber1[i] = Enumber1[i].substring(0,lastLash);
    } 

   $(".cut").append("<p>"+(i+1)+" "+Enumber1[i]+"</p>");
  } 
  });

成为你的HTML:

<div class="title">
Title1 - Title2 [XXX]
</div>
<div class="cut">
</div>