javascript 我克隆的部分总是出现在页面的底部

xpcnnkqh  于 2023-04-04  发布在  Java
关注(0)|答案(1)|浏览(133)

我有代码克隆的一部分,从形式。但当我点击克隆按钮,克隆的部分总是出现在底部,我希望克隆的一个是正确的克隆后。
下面是我代码:http://jsfiddle.net/ef654xto/

function remove() {
    var section = this.closest("section");
    section.parentNode.removeChild(section);
}

function duplicate() {
    var section = this.closest("section").cloneNode(true);
    setupHandlers(section);
    document.body.appendChild(section);
    section.querySelector(".form-remove").style.display = "";
    section.querySelector(".form-duplicate").style.display = "none";
}

function setupHandlers(section) {
  section.querySelector(".form-duplicate").addEventListener("click", duplicate);
  section.querySelector(".form-remove").addEventListener("click", remove);
}

// Setup handlers on any initially-present forms
document.querySelectorAll(".wrapper").forEach(setupHandlers);
<section class="wrapper" >
  <button type="button" class="btn btn-primary form-duplicate">Add New</button><br><br>
  <div class="wrapper">
    <div class="row-fluid">
    <div class="form-row box template">
    <div class="form-group col-md-2">
      <label for="inputCity">Traveler</label>
      <input type="text" class="form-control" id="inputCity" required>
    </div>
    <div class="form-group col-md-1-half">
      <label for="inputZip">Cit of Origin</label>
      <input type="text" class="form-control" id="inputZip" required>
    </div>
    <div class="form-group col-1-half">
      <label for="inputZip">Cit of Destination</label>
      <input type="text" class="form-control" id="inputZip" required>
    </div>
    <div class="form-group col-1-3qtr">
      <label for="inputZip">Departure Date</label>
      <input type="date" class="form-control" id="inputZip" required>
    </div>
    <div class="form-group col-1-1qtr">
      <label for="appt">Departure Time</label>
      <input type="time" class="form-control" id="inputZip" required>
    </div>
    <div class="form-group col-md-1">
      <label for="inputState" class="form-label">Hotel</label>
      <select class="form-control" id="inputState" required>
        <option selected disabled value="">Choose</option>
        <option>Yes</option>
        <option>No</option>
      </select>
    </div>
    <div class="form-group col-0-half">
      <label for="inputZip">Day</label>
      <input type="text" class="form-control" id="inputZip" required>
    </div>
    <div>
      <button class="p-1 form-remove" style="background-color: red;display:none;margin-top: 33px;width: 40px;height: 37px;border-radius: 5px;border: none;">
        <lord-icon
            src="https://cdn.lordicon.com/gsqxdxog.json"
            trigger="hover"
            colors="primary:#ffffff,secondary:#ffffff"
            scale="60"
            state="hover-empty"
            style="width:25px;height:25px;margin-top: 2%;">
        </lord-icon>
      </button>
    </div>
  </section>

  <div class="form-group col-md-13">
    <label for="exampleFormControlTextarea1">Comment</label>
    <textarea class="form-control" id="exampleFormControlTextarea1" rows="3" required></textarea>
</div>
<div class="row">
    <div class="form-group col-md-6">
        <label for="inputState" class="form-label">Trasportation</label>
        <input type="file" class="custom-file-input" id="validatedCustomFile" required>
        <label class="custom-file-label" for="validatedCustomFile">Brosur Event</label>
        <div class="invalid-feedback">Example invalid custom file feedback</div>
        </select>
    </div>
    <div class="form-group col-md-6">
        <label for="inputState" class="form-label">Purpose</label>
        <input type="file" class="custom-file-input" id="validatedCustomFile" required>
        <label class="custom-file-label" for="validatedCustomFile">Surat Penunjukan</label>
        <div class="invalid-feedback">Example invalid custom file feedback</div>
    </div>
</div>
<div class="row">
    <div class="form-group col-md-6">
        <label for="inputState" class="form-label">Trasportation</label>
        <input type="file" class="custom-file-input" id="validatedCustomFile" required>
        <label class="custom-file-label" for="validatedCustomFile">Surat Permintaan Sponsor Dari
            RS/Asosiasi</label>
        <div class="invalid-feedback">Example invalid custom file feedback</div>
        </select>
    </div>
    <div class="form-group col-md-6">
        <label for="inputState" class="form-label">Purpose</label>
        <input type="file" class="custom-file-input" id="validatedCustomFile" required>
        <label class="custom-file-label" for="validatedCustomFile">KTP/Paspor Traveler</label>
        <div class="invalid-feedback">Example invalid custom file feedback</div>
    </div>
</div>

我通过互联网搜索,并没有发现任何请帮助我,任何形式的帮助是感激。谢谢。

bvuwiixz

bvuwiixz1#

确保每一个打开的html标签,如<div>也有一个关闭标签。 这里是一个清理版本的代码剪辑,我删除了“未打开”的标签,并关闭了所有未关闭的`标签。

<section class="wrapper">
  <button type="button" class="btn btn-primary form-duplicate">Add New</button
  ><br /><br />
  <div class="wrapper">
    <div class="row-fluid">
      <div class="form-row box template">
        <div class="form-group col-md-2">
          <label for="inputCity">Traveler</label>
          <input type="text" class="form-control" id="inputCity" required />
        </div>
        <div class="form-group col-md-1-half">
          <label for="inputZip">Cit of Origin</label>
          <input type="text" class="form-control" id="inputZip" required />
        </div>
        <div class="form-group col-1-half">
          <label for="inputZip">Cit of Destination</label>
          <input type="text" class="form-control" id="inputZip" required />
        </div>
        <div class="form-group col-1-3qtr">
          <label for="inputZip">Departure Date</label>
          <input type="date" class="form-control" id="inputZip" required />
        </div>
        <div class="form-group col-1-1qtr">
          <label for="appt">Departure Time</label>
          <input type="time" class="form-control" id="inputZip" required />
        </div>
        <div class="form-group col-md-1">
          <label for="inputState" class="form-label">Hotel</label>
          <select class="form-control" id="inputState" required>
            <option selected disabled value="">Choose</option>
            <option>Yes</option>
            <option>No</option>
          </select>
        </div>
        <div class="form-group col-0-half">
          <label for="inputZip">Day</label>
          <input type="text" class="form-control" id="inputZip" required />
        </div>
        <div>
          <button
            class="p-1 form-remove"
            style="
              background-color: red;
              display: none;
              margin-top: 33px;
              width: 40px;
              height: 37px;
              border-radius: 5px;
              border: none;
            "
          >
            <lord-icon
              src="https://cdn.lordicon.com/gsqxdxog.json"
              trigger="hover"
              colors="primary:#ffffff,secondary:#ffffff"
              scale="60"
              state="hover-empty"
              style="width: 25px; height: 25px; margin-top: 2%"
            >
            </lord-icon>
          </button>
        </div>
      </div>
    </div>
  </div>
</section>

<div class="form-group col-md-13">
  <label for="exampleFormControlTextarea1">Comment</label>
  <textarea
    class="form-control"
    id="exampleFormControlTextarea1"
    rows="3"
    required
  ></textarea>
</div>
<div class="row">
  <div class="form-group col-md-6">
    <label for="inputState" class="form-label">Trasportation</label>
    <input
      type="file"
      class="custom-file-input"
      id="validatedCustomFile"
      required
    />
    <label class="custom-file-label" for="validatedCustomFile"
      >Brosur Event</label
    >
    <div class="invalid-feedback">Example invalid custom file feedback</div>
  </div>
  <div class="form-group col-md-6">
    <label for="inputState" class="form-label">Purpose</label>
    <input
      type="file"
      class="custom-file-input"
      id="validatedCustomFile"
      required
    />
    <label class="custom-file-label" for="validatedCustomFile"
      >Surat Penunjukan</label
    >
    <div class="invalid-feedback">Example invalid custom file feedback</div>
  </div>
</div>
<div class="row">
  <div class="form-group col-md-6">
    <label for="inputState" class="form-label">Trasportation</label>
    <input
      type="file"
      class="custom-file-input"
      id="validatedCustomFile"
      required
    />
    <label class="custom-file-label" for="validatedCustomFile"
      >Surat Permintaan Sponsor Dari RS/Asosiasi</label
    >
    <div class="invalid-feedback">Example invalid custom file feedback</div>
  </div>
  <div class="form-group col-md-6">
    <label for="inputState" class="form-label">Purpose</label>
    <input
      type="file"
      class="custom-file-input"
      id="validatedCustomFile"
      required
    />
    <label class="custom-file-label" for="validatedCustomFile"
      >KTP/Paspor Traveler</label
    >
    <div class="invalid-feedback">Example invalid custom file feedback</div>
  </div>
</div>

相关问题