javascript - 如何在函数中插入while循环
问题描述
我正在尝试在名为 myFunction 的函数中插入一个 while 循环。当您单击按钮时,它应该显示数组结果。
<!DOCTYPE html>
<html>
<body>
<button id="btn">Click Me!</button>
<p id="i"><strong>this</strong> represents:</p>
<p id="demo"></p>
<script>
function myFunction () {
let i = 0;
while(i<5){
document.write(i + '<br/>');
i++;
}
}
var btn = document.getElementById("btn");
btn.addEventListener("click", myFunction());
</script>
</body>
</html>
解决方案
您在 myFunction 之后缺少 ():
function myFunction() {
let i = 0;
while(i<5){
document.write(i + '<br/>');
i++;
}
}
你确定要使用document.write
而不是console.log(i)
?
推荐阅读
- ios - Swift CMPedometerData 如何更新每一步,而不是每隔几秒延迟和更新步数?
- c# - .net 5.0 windows 服务不自动启动
- twitter-bootstrap - 如何自动将 Twitter 特定用户的消息推送到 WhatsApp 组
- c# - Xamarin 将字符串添加到 mvvm 中的条目
- python - 实时读取多个日志文件
- java - 未找到给定的属性文件
- python - 如何在python中使用正则表达式来获取产品名称中的内存详细信息
- html - 是否可以上传自定义字体以在任何本地 HTML 文件中使用?
- mysql - 我可以删除旧的 MySQL 服务吗?
- autodesk-forge - 调用 Autodesk forge API 时出现异常