Javascript if语法
当我试图运行一个简单的if语句时,我有语法错误
[打破此错误]});
无效的赋值左侧[Break On This Error]容器+ =
我的问题是什么以及如何制作:
if this.ewCount != 0 then {} elseif NotDoneh == 0 then {} ELSE {}
这是我目前的代码:
var conta = ''; $.each(items, function () { if (this.ewCount != 0) { if (DoneWidth == 0) { conta += '
dddddddddddddddddd
' + }); if (NotDoneh == 0) { conta += '
dddddddddddddddddd
' + }); }); container += '' +
拿你的伪代码:
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 += '
dddddddddddddddddd
'; } } else if (NotDoneh == 0) { conta += '
dddddddddddddddddd
'; } else { // you don't seem to have any code to go in the final else, // unless it was meant to be container += ''; } container += ''; }); // note here }); is correct because the } closes the anonymous function // and then the ); finishes off the $.each(...
你可以将它与我在上面显示的if / else结构放在一起,和/或以其他方式移动东西,以便正确的代码位于右边的if或else。
删除if块的尾随花括号后的括号。
if (NotDoneh == 0) { conta += '
dddddddddddddddddd
' + });
应该
if (NotDoneh == 0) { conta += '
dddddddddddddddddd
' + } // <-- No ); <-- This is not a smiley, but a parenthesis + semicolon.
if (this.ewCount != 0) { if (DoneWidth == 0) { conta += '
dddddddddddddddddd
'; } if (NotDoneh == 0) { conta += '
dddddddddddddddddd
'; } }
elseif NotDoneh == 0 then {}
您在JS中没有elseif
, else if
相反,您应该使用else if
:
else if NotDoneh == 0 then {}
var conta = ''; $.each(items, function () { if (this.ewCount != 0) { if (DoneWidth == 0) { conta += '
dddddddddddddddddd
' } if (NotDoneh == 0) { conta += '
dddddddddddddddddd
' } } else{ //here you do the else } });