首页 > 解决方案 > 在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>

标签: javascriptfor-loop

解决方案


这是您想要的 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>


推荐阅读