Javascript if syntax
Javascript if syntax
当我尝试运行一个简单的 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 }
});
相关文章:
- 可以't让我的if语句处理js中的html表单输入
- 如果 a 为 false,则 if(a) === if(false)
- 如何在ReactJS JSX中执行嵌套的if-else语句
- 我已经创建了一个jquery转盘,并使用if条件来运行和停止转盘
- 正在尝试使用if和else添加类,但无法正常工作
- 在PHP中使用javascript更改页面标题'if'
- gulp-if-javascript文件,但不在gulp-useref的特定目录中
- 如何在 API 调用后和 if 语句中启用提交按钮
- 带有多个elseif的Javascript if语句
- 什么是&&在没有if的行中的变量之间
- 另一个if(!$scope.$$phase)$scope$apply()szenario-需要帮助才能通关
- Sharepoint JScript if语句未执行
- Javascript:If-then语句在函数中不起作用
- for循环中的javascript if语句找不到==
- jquery if语句返回return wong语句
- 对于loop.if-仅在经过所有间隔后返回true
- Javascript Return and if/else
- Javascript nested if
- 在if语句下的html中使用javascript变量
- Javascript if syntax