【发布时间】:2015-11-10 23:34:28
【问题描述】:
我正在尝试创建一个运行 while 循环的函数。
它应该运行以下内容:
- 当循环中的数字是3的倍数时,将参数string1附加到div
- 当循环中的数字是5的倍数时,将参数string2附加到div
- 否则,在下面附加另一个字符串
对于脚本标记中以“for (x % 3 == 0) {”开头的行,我收到 Chrome 工具箱错误“Uncaught SyntaxError: Unexpected token)”。不知道为什么……
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<div id="output"></div>
<script src="https://code.jquery.com/jquery-2.1.1.js"></script>
<script>
var this_that = function(string1, string2) {
var x = 1
while (x <= 100) {
for (x % 3 == 0) {
$(#output).append(x, string1)
} else if (x % 5 == 0) {
$(#output).append(x, string2)
} else {
$(#output).append(x,'is not a multiple of 3 or 5')
}
x++
}
}
</script>
【问题讨论】:
-
应该是
if (x % 3 == 0)for 的语法是for ([initialization]; [condition]; [final-expression]){ statement}我强烈推荐你学习基础For Loop
标签: javascript jquery function for-loop while-loop