如何创建最初有 N 个 div 的 HTML 页面

我正在制作一个类似于具有多个步骤的表单的页面。


但在此之前,我从之前的表单中取出一个数字 N,它代表表单的步骤数。


每个步骤都需要一个“tab”类的特定 div。现在,我需要使用 Javascript 或 PHP 来将主 div 复制 N 次。


<form id="regForm" action="/action_page.php">


    <!-- One "tab" for each step in the form: -->

    <div class="tab">

        <h1>Register:</h1>

        Name:

        <p><input placeholder="First name..." oninput="this.className = ''" name="fname"></p>

        <p><input placeholder="Last name..." oninput="this.className = ''" name="lname"></p>

    </div> 

</form>


繁花不似锦
浏览 132回答 2
2回答

RISEBY

此代码克隆 div N 次<html><body>&nbsp; &nbsp; <div class="tab" id="id0">&nbsp; &nbsp; &nbsp; &nbsp; <p>Test Tab</p>&nbsp; &nbsp; </div>&nbsp;<script>&nbsp; &nbsp; // Get the element&nbsp; &nbsp; var elem = document.querySelector('#id0');&nbsp; &nbsp; //set n to the value form the previous page&nbsp; &nbsp; var n = 10;&nbsp; &nbsp; var i;&nbsp; &nbsp; for (i = 1; i < n; i++) {&nbsp; &nbsp; &nbsp; &nbsp; // Create a copy of it&nbsp; &nbsp; &nbsp; &nbsp; var clone = elem.cloneNode(true);&nbsp; &nbsp; &nbsp; &nbsp; // Update the ID&nbsp; &nbsp; &nbsp; &nbsp; clone.id = "id" + n;&nbsp; &nbsp; &nbsp; &nbsp; // Inject it into the DOM&nbsp; &nbsp; &nbsp; &nbsp; elem.after(clone);&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;</script></body></html>

12345678_0001

您可以占用一个 for 并重复您使用 POST 或 GET 带来的步骤数的 div<!-- One "tab" for each step in the form: --><?php&nbsp;&nbsp; &nbsp; $divs = $_POST('number_divs');&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; for ($i = 1; $i <= $divs; $i++) {&nbsp;&nbsp; &nbsp;&nbsp;?><div class="tab">&nbsp; &nbsp; <h1>Register:</h1>&nbsp; &nbsp; Name:&nbsp; &nbsp; <p><input placeholder="First name..." oninput="this.className = ''" name="fname"></p>&nbsp; &nbsp; <p><input placeholder="Last name..." oninput="this.className = ''" name="lname"></p></div>&nbsp;<?php }&nbsp; ?>
打开App,查看更多内容
随时随地看视频慕课网APP