首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >If,else if循环中断

If,else if循环中断
EN

Stack Overflow用户
提问于 2013-05-01 22:30:44
回答 3查看 153关注 0票数 0

我刚刚写了一个小的if,else if循环,我在控制台中得到了一个unexpected token ';'错误。如果我删除每个$(this).css('top', top-3"em");语句后面的;,我会得到另一个错误,说明else if之前的}是意外的!

这是我的循环,有什么想法我可能迷路了吗?

代码语言:javascript
运行
复制
$('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");
    }

});
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-05-01 22:32:14

你有很少的语法错误,可能需要纠正更多的地方。

  • 您需要使用相等运算符===而不是赋值运算符=
  • 您在最后一条if语句中遗漏了if。
  • 您必须使用+来连接字符串<+>D9将是代码

这是无语法错误的

代码语言:javascript
运行
复制
$('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");
    }    
});
票数 1
EN

Stack Overflow用户

发布于 2013-05-01 22:32:02

您正在设置resultcount变量,但没有对其进行检查:

代码语言:javascript
运行
复制
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");
}

使用===检查结果和类型。

还有很多语法错误,试着用开发人员工具调试它们。

票数 3
EN

Stack Overflow用户

发布于 2013-05-01 22:33:18

如果您忘记在每个css参数中添加+,那么您应该使用==而不是= inside。

代码语言:javascript
运行
复制
  $('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");
        } 

    });
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16320052

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档