2016-10-05 108 views
0

我正在尋找一種將特定內容放入Bootstrap模式而不重複代碼的方法。引導模式:構建動態內容

<!-- Modal --> 
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"> 
    <div class="modal-dialog" role="document"> 
    <div class="modal-content"> 
     <div class="modal-header"> 
     <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button> 
     <h4 class="modal-title" id="myModalLabel">Modal title</h4> 
     </div> 
     <div class="modal-body"> 
     ... 
     </div> 
     <div class="modal-footer"> 
     <button type="button" class="btn btn-default" data-dismiss="modal">Close</button> 
     <button type="button" class="btn btn-primary">Save changes</button> 
     </div> 
    </div> 
    </div> 
</div> 

<ul> 
    <li data-toggle="modal" data-target="#myModal" data-content="Content of Text1">Text1</li> 
    <li data-toggle="modal" data-target="#myModal" data-content="Content of Text2">Text2</li> 
    <li data-toggle="modal" data-target="#myModal" data-content="Content of Text3">Text3</li> 
</ul> 

因此,這意味着該模式應該是足夠的智慧理解(文本,文本2或文本3)點擊了哪個項目,並顯示存儲在屬性數據內容的內容。

我該怎麼做?

+0

它會告訴你如何做到這一點的引導文件:http://getbootstrap.com/javascript/#modals-related-target –

+0

有你讀了一節[不同模態內容用觸發器](http://getbootstrap.com/javascript/#modals-related-target)? – manix

回答

2

模態體將data-content屬性值的點擊li元素。

請檢查下面的代碼段。

$("li[data-target='#myModal']").on('click',function(){ 
 
    $(".modal-body").html($(this).attr('data-content')); 
 
});
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> 
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> 
 
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> 
 
<!-- Modal --> 
 
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"> 
 
    <div class="modal-dialog" role="document"> 
 
    <div class="modal-content"> 
 
     <div class="modal-header"> 
 
     <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button> 
 
     <h4 class="modal-title" id="myModalLabel">Modal title</h4> 
 
     </div> 
 
     <div class="modal-body"> 
 
     ... 
 
     </div> 
 
     <div class="modal-footer"> 
 
     <button type="button" class="btn btn-default" data-dismiss="modal">Close</button> 
 
     <button type="button" class="btn btn-primary">Save changes</button> 
 
     </div> 
 
    </div> 
 
    </div> 
 
</div> 
 

 
<ul> 
 
    <li data-toggle="modal" data-target="#myModal" data-content="Content of Text1">Text1</li> 
 
    <li data-toggle="modal" data-target="#myModal" data-content="Content of Text2">Text2</li> 
 
    <li data-toggle="modal" data-target="#myModal" data-content="Content of Text3">Text3</li> 
 
</ul>

+1

謝謝,這對我有用! – Reza