javascript - 在for循环中显示计数器的正确javascript是什么
问题描述
我一直使用 php 来回显循环递增的变量。现在我想使用更简单的纯javascript。
而不是这个:
<script>
<?php
echo "<script>";
for ($i = 1; $i <= 48; $i++) {
echo 'X'.$i;
echo '}';
echo "</script>";
?>
我只想要这个:
<script>
for (i = 1; i <= 48; i++) {
document.write (X[i]); //What is the correct syntax for this?
}
</script>
解决方案
这是您想要的 O/P。使用“+”连接。
function hello()
{
for (i = 1; i <= 48; i++)
{ document.write ('X'+i); }
}
<!DOCTYPE html>
<html>
<head>
<title></title>
<script type="text/javascript">
</script>
</head>
<body onload="hello();">
</body>
</html>
推荐阅读
- sql-server - Flyway 是否有我们可以在 SQL 迁移中使用的“数据库名称”占位符?
- android - 如何在 android 和 ios 上运行 Angular 7 应用程序?
- xamarin - Xamarin.Forms 应用恢复时恢复导航堆栈
- python - 我可以在没有 Python 文件的情况下创建 sqlite3 连接吗?
- excel - 是否有一个公式可以根据当前选项卡上的输入让单元格在另一个选项卡上显示单元格的内容?
- javascript - Aframe glTF 不显示
- php - 两个链接随机php
- flutter - Flutter 调试与发布版本
- javascript - 如何解决typeError,猫鼬?
- asp.net-core-mvc - 如何在多个视图中重用 html 代码?