2017-06-19 64 views
0

我有以下HMTL:使用prependTo元素消失

<div class="modal fade in" id="data-confirmation-modal" tabindex="-1" role="dialog" aria-labelledby="data-confirmation-modal-label"> 
     <div class="modal-dialog" role="document"> 
     <div class="modal-content"> 
      <div class="modal-header"> 
      <span class="fa fa-exclamation-triangle"></span> 
      <h4 class="modal-title" id="data-confirmation-modal-label">Titile</h4> 
      </div> 
      <div class="modal-body"> 
      <p>Example</p> 
      </div> 
      <div class="modal-footer"> 
      <button id="" type="button" class="btn-confirm">Conform</button> 
       <button type="button" class="btn-cancel" autofocus="" data-dismiss="modal">Avbryt</button> 
      </div> 
     </div> 
     </div> 
</div> 

我想創建一個表單元素,並把表單元素內模態含量javascipt的:

$form = $("<form></form>"); 
$form.prependTo($('.modal-content')); 

當我使用上面的代碼我模態是空的。希望得到一些幫助。 我想的HTML如下:

<div class="modal fade in" id="data-confirmation-modal" tabindex="-1" role="dialog" aria-labelledby="data-confirmation-modal-label"> 
      <div class="modal-dialog" role="document"> 
      <div class="modal-content"> 
     <form> 
       <div class="modal-header"> 
       <span class="fa fa-exclamation-triangle"></span> 
       <h4 class="modal-title" id="data-confirmation-modal-label">Titile</h4> 
       </div> 
       <div class="modal-body"> 
       <p>Example</p> 
       </div> 
       <div class="modal-footer"> 
       <button id="" type="button" class="btn-confirm">Conform</button> 
        <button type="button" class="btn-cancel" autofocus="" data-dismiss="modal">Avbryt</button> 
       </div> 
     </form> 
      </div> 
      </div> 
    </div> 
+1

如果我沒有弄錯,['prependTo()'](http://api.jquery.com/prependto/)不用於包裝。它用於在目標元素的開始處添加/傳輸另一個元素 – Swellar

回答

0

你可以使用jQuery包裹http://api.jquery.com/wrap/ 包裝.modal內容與形式,也可以重寫整個HTML,但與形式,像

$('.modal-content').html('<form>'+$('.modal-content').html()+'</form>') 
2

$(".modal-content").children().wrapAll("<form></form>")
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="modal fade in" id="data-confirmation-modal" tabindex="-1" role="dialog" aria-labelledby="data-confirmation-modal-label"> 
 
     <div class="modal-dialog" role="document"> 
 
     <div class="modal-content"> 
 
      <div class="modal-header"> 
 
      <span class="fa fa-exclamation-triangle"></span> 
 
      <h4 class="modal-title" id="data-confirmation-modal-label">Titile</h4> 
 
      </div> 
 
      <div class="modal-body"> 
 
      <p>Example</p> 
 
      </div> 
 
      <div class="modal-footer"> 
 
      <button id="" type="button" class="btn-confirm">Conform</button> 
 
       <button type="button" class="btn-cancel" autofocus="" data-dismiss="modal">Avbryt</button> 
 
      </div> 
 
     </div> 
 
     </div> 
 
</div>

  1. 使用.wrapAll()來包裝所有內容
0

您可以使用以下代碼。

$(".modal-content").wrap("<form></form>").