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

我正在做一个页面,就像一个带有多个步骤的表单。


但是在此之前我从以前的表格中取一个数字 N,它代表表格的步数。


每个步骤都需要一个特定的 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>


哔哔one
浏览 176回答 2
2回答

慕的地8271018

此代码克隆 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>

MMTTMM

您可以使用 POST 或 GET 占用一个 for 并重复 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

相关分类

JavaScript