我有代码切割瓷砖: 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');
}
});
帮我解决问题
感谢!!!
答案 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>