Javascript if syntax

Javascript if syntax

本文关键字:syntax if Javascript      更新时间:2023-09-26

当我尝试运行一个简单的 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 块尾随大括号后面的括号。

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

应该是

 if (NotDoneh == 0) {   conta += '<br/>dddddddddddddddddd<br/><br/>' + 
 } // <-- No ); <-- This is not a smiley, but a parenthesis + semicolon.

获取伪代码:

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或其他。

if (this.ewCount != 0) {
  if (DoneWidth == 0) {
    conta += '<br/>dddddddddddddddddd<br/><br/>';
  }
  if (NotDoneh == 0) {
    conta += '<br/>dddddddddddddddddd<br/><br/>';
  }
}
elseif NotDoneh == 0 then {} 

你在 JS 中没有elseif,你应该改用else if

else if NotDoneh == 0 then {} 
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/>'
           }
        }  
       else{ //here you do the else  }
     });