首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > JavaScript >

简写上面的js代码

2012-09-23 
简写下面的js代码for(i0inews_count.toString().lengthi++){var i1i+1$(.connotice).append(li

简写下面的js代码
for(i=0;i<news_count.toString().length;i++){
var i1=i+1;
$(".connotice").append('<li>'+i1+'.<a href="#">立即修改</a></li>');

}
if(news_count.toString().length==3){
$('.connotice li:eq(0)').append(newsjson[0].newscont);
$('.connotice li:eq(0) a').attr('href',newsjson[0].newshref.mmt);
$('.connotice li:eq(1)').append(newsjson[1].newscont);
$('.connotice li:eq(1) a').attr('href',newsjson[1].newshref.mmt);
$('.connotice li:eq(2)').append(newsjson[2].newscont);
$('.connotice li:eq(2) a').attr('href',newsjson[2].newshref.mmt);
}
else if(news_count.toString().length==1)
{
$('.connotice li').append(newsjson[news_count-1].newscont);
$('.connotice li a').attr('href',newsjson[news_count-1].newshref.mmt);
}
else if(news_count.toString().length==2){
var news_count1=news_count.toString().substring(0,1);
var news_count2=news_count.toString().substring(1,2);
alert(news_count1);
alert(news_count2);
$('.connotice li:eq(0)').append(newsjson[news_count1-1].newscont);
$('.connotice li:eq(0) a').attr('href',newsjson[news_count1-1].newshref.mmt);
$('.connotice li:eq(1)').append(newsjson[news_count2-1].newscont);
$('.connotice li:eq(1) a').attr('href',newsjson[news_count2-1].newshref.mmt);
}
}

[解决办法]

JScript code
    var news_count1=0,news_count2=0;    if (news_count.length == 2) {        news_count1 = news_count.toString().substring(0, 1);        news_count2 = news_count.toString().substring(1, 2);    }    for (var i = 1; i <= news_count.length; i++) {        $(".connotice").append('<li>' + i + '.<a href="#">立即修改</a></li>');        if (news_count.length == 2) {            var _count=i==0?news_count1:news_count2;            $('.connotice li').eq(i-1).append(newsjson[_count-1].newscont).find("a").attr('href', newsjson[_count-1].newshref.mmt);        }else{            $('.connotice li').eq(i-1).append(newsjson[i-1].newscont).find("a").attr('href', newsjson[i-1].newshref.mmt);        }    } 

热点排行
Bad Request.