【问题标题】:how to use the for loop in html template如何在html模板中使用for循环
【发布时间】:2021-03-24 20:29:13
【问题描述】:

不知道如何向模板添加循环。 有人可以举个例子来说明如何在模板中使用 for 循环的语法吗?

<!DOCTYPE HTML>
 <html lang="en">
<head>
 <title>This page was generated from a template</title>
</head>
<body>

{{loop}}
<h6>{{content_from_data_structure}}</h6>
{{end_loop}}

</body>
</html>

【问题讨论】:

    标签: html templates


    【解决方案1】:

    HTML 是一种标记语言,您不能在其中创建对象或循环。您可以使用 PHP 或 Javascript 来创建对象并将其插入 HTML。

    PHP 是这样的;

    <html>
        <body>
            <?php 
                for ($x = 0; $x <= 10; $x++) 
                {
                    echo "<h1>The number is: $x</h1>";
                }
            ?>
        </body>
    </html>
    

    使用 Javascript;

    <html>
        <body>
            <div id="insertHere"></div>
            <script>
                var text = "";
                var i;
                for (i = 0; i < 10; i++) 
                {
                    text += "<h1>The number is: " + i.toString() + "<br>";
                }
                document.getElementById("insertHere").innerHTML = text;
            </script>
        </body>
    </html>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-10
      • 2013-10-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多