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

就高手精简一下代码解决思路

2012-06-08 
就高手精简一下代码JScript codeif ((index 5 && eNum 3)) {alert(Repeat)index 0//var index

就高手精简一下代码

JScript code
        if ((index <= 5 && eNum == 3)) {            alert("Repeat");            index = 0;            //            var index_ = "";            errors = [];            eNum = 0;            $("span.sp3").html(eNum);        }        if ((index > 5 && index <= 10 && eNum == 5)) {            alert("Repeat");            index = 0;            //            var index_ = "";            errors = [];            eNum = 0;            $("span.sp3").html(eNum);        }        if ((index > 10 && index <= 15 && eNum == 7)) {            alert("Repeat");            index = 0;            //            var index_ = "";            errors = [];            eNum = 0;            $("span.sp3").html(eNum);        }

其实if大括号内的内容是一样的。
index是5的倍数。
eNum=index*0.4+1

[解决办法]
var n = 5;
if ((index <= n*1 && eNum == 3) || (index > n*1 && index <= n*2 && eNum == 5) || (index > n*2 && index <= n*3 && eNum == 7)) { 


[解决办法]
你这个eNum就没用啊,js是有取余的功能的,楼主想多了啊,直接这样就行了啊
if(index % 5==0){
var eNum=eval(index*0.4+1);
alert("Repeat");
index = 0;
// var index_ = "";
errors = [];
eNum = 0;
$("span.sp3").html(eNum);
}

再精简一下就是这样
if(index % 5==0){
alert("Repeat");
index = 0;
// var index_ = "";
errors = [];
$("span.sp3").html(0);
}

热点排行