我刚刚写了一个小的if,else if循环,我在控制台中得到了一个unexpected token ';'
错误。如果我删除每个$(this).css('top', top-3"em");
语句后面的;
,我会得到另一个错误,说明else if
之前的}
是意外的!
这是我的循环,有什么想法我可能迷路了吗?
$('div.result').each(function() {
$(this).css('top', top+"em");
top = top + 8;
resultcount = resultcount - 1;
if(resultcount=5) {
$(this).css('top', top-3"em");
} else if(resultcount=4) {
$(this).css('top', top-7"em");
} else if(resultcount=3) {
$(this).css('top', top-14"em");
} else if(resultcount=2) {
$(this).css('top', top-20"em");
} else(resultcount=1) {
$(this).css('top', top-30"em");
}
});
发布于 2013-05-01 22:32:14
你有很少的语法错误,可能需要纠正更多的地方。
===
而不是赋值运算符=
+
来连接字符串<+
>D9将是代码这是无语法错误的
$('div.result').each(function() {
$(this).css('top', top+"em");
top = top + 8;
resultcount = resultcount - 1;
if(resultcount===5) {
$(this).css('top', (top-3)+"em");
} else if(resultcount===4) {
$(this).css('top', (top-7)+"em");
} else if(resultcount===3) {
$(this).css('top', (top-14)+"em");
} else if(resultcount===2) {
$(this).css('top', (top-20)+"em");
} else if(resultcount===1) {
$(this).css('top', top-30+"em");
}
});
发布于 2013-05-01 22:32:02
您正在设置resultcount变量,但没有对其进行检查:
if(resultcount===5) {
$(this).css('top', "top-3em");
}
else if(resultcount===4) {
$(this).css('top', "top-7em");
}
else if(resultcount===3) {
$(this).css('top', "top-14em");
}
else if(resultcount===2) {
$(this).css('top', top-20"em");
}
else {
$(this).css('top', "top-30em");
}
使用===
检查结果和类型。
还有很多语法错误,试着用开发人员工具调试它们。
发布于 2013-05-01 22:33:18
如果您忘记在每个css参数中添加+,那么您应该使用==而不是= inside。
$('div.result').each(function() {
$(this).css('top', top + " em");
top = top + 8;
resultcount = resultcount - 1;
if(resultcount ==5) {
$(this).css('top', top-3 + "em");
} else if(resultcount==4) {
$(this).css('top', top-7 + "em");
} else if(resultcount==3) {
$(this).css('top', top-14 + "em");
} else if(resultcount==2) {
$(this).css('top', top-20 + "em");
}
});
https://stackoverflow.com/questions/16320052
复制相似问题