Javascript if语法 [英] Javascript if syntax

查看:69
本文介绍了Javascript if语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试运行简单的if语句时出现语法错误

[打破此错误]});

无效分配左侧 [中断此错误]容器+ =

我的问题是什么 以及我的制作方法:

if this.ewCount != 0 then {}  
elseif NotDoneh == 0 then {} 
ELSE {}

这是我当前的代码:

var conta = '<div>';
$.each(items, function () {
    if (this.ewCount != 0) {

        if (DoneWidth == 0) {
            conta += '<br/>dddddddddddddddddd<br/><br/>' +
        });
        if (NotDoneh == 0) {
            conta += '<br/>dddddddddddddddddd<br/><br/>' +
        });
    });

    container += '</div>' +

解决方案

获取您的伪代码:

if this.ewCount != 0 then {}  
elseif NotDoneh == 0 then {} 
ELSE {}

并将其转换为JavaScript:

if (this.ewCount != 0) {
   // do something
} else if (NotDoneh == 0) {
   // do something else
} else {
   // do something else again
}

在您的实际JS中,存在几个问题,主要是您在某些行的末尾加上了+运算符,之后没有其他运算符,并且在需要时用});关闭了每个if语句.刚刚有了}-我想您已经将需要关闭$.each的方式混为一谈,因为它是一个以函数作为参数的函数调用,因此它必须为$.each(items,function() { });.

我不确定如何重写您的JS,因为它具有if (DoneWidth == 0)测试,该测试不在您的伪代码中-该测试应该嵌套在第一个if或...内吗?无论如何,如果将其更新为如下所示,则即使它不是正确的算法,它也至少应该是有效的:

$.each(items, function () {
    if (this.ewCount != 0) {
        // note the following if is nested inside the one above: not sure
        // if that's what you intended, but it gives a good example of
        // how to do something like that
        if (DoneWidth == 0) {
            conta += '<br/>dddddddddddddddddd<br/><br/>';
        }
    } else if (NotDoneh == 0) {
            conta += '<br/>dddddddddddddddddd<br/><br/>';
    } else {
        // you don't seem to have any code to go in the final else,
        // unless it was meant to be
        container += '</div>';
    }

    container += '</div>';
 }); // note here }); is correct because the } closes the anonymous function
     // and then the ); finishes off the $.each(...

您可以将其与我上面显示的if/else结构放在一起,和/或以其他方式移动事物,以便正确的代码最终出现在正确的if或else内部.

i have syntax error when i'm trying to run a simple if statements

[Break On This Error] });

invalid assignment left-hand side [Break On This Error] container +=

what is my problem and how i can make:

if this.ewCount != 0 then {}  
elseif NotDoneh == 0 then {} 
ELSE {}

this is my current code:

var conta = '<div>';
$.each(items, function () {
    if (this.ewCount != 0) {

        if (DoneWidth == 0) {
            conta += '<br/>dddddddddddddddddd<br/><br/>' +
        });
        if (NotDoneh == 0) {
            conta += '<br/>dddddddddddddddddd<br/><br/>' +
        });
    });

    container += '</div>' +

解决方案

To take your pseudo-code:

if this.ewCount != 0 then {}  
elseif NotDoneh == 0 then {} 
ELSE {}

and turn it into JavaScript:

if (this.ewCount != 0) {
   // do something
} else if (NotDoneh == 0) {
   // do something else
} else {
   // do something else again
}

In your actual JS there are several problems, mainly that you've got the + operator on the end of some lines without another operator after it, and you've closed each of your if statements with }); when they should've just had } - I think you've got that mixed up with how you need to close the $.each since it is a function call with a function as a parameter so it needs to be $.each(items,function() { });.

I'm not sure how to rewrite your JS, because it has an if (DoneWidth == 0) test that isn't in your pseudo-code - is that supposed to be nested inside the first if, or...? Anyway, if you update it to look like the following it should at least be valid, even if not the right algorithm:

$.each(items, function () {
    if (this.ewCount != 0) {
        // note the following if is nested inside the one above: not sure
        // if that's what you intended, but it gives a good example of
        // how to do something like that
        if (DoneWidth == 0) {
            conta += '<br/>dddddddddddddddddd<br/><br/>';
        }
    } else if (NotDoneh == 0) {
            conta += '<br/>dddddddddddddddddd<br/><br/>';
    } else {
        // you don't seem to have any code to go in the final else,
        // unless it was meant to be
        container += '</div>';
    }

    container += '</div>';
 }); // note here }); is correct because the } closes the anonymous function
     // and then the ); finishes off the $.each(...

You can put that together with the if/else structure that I showed above, and/or otherwise move things around so the right bit of code ends up inside the right if or else.

这篇关于Javascript if语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆