我正在寻找一种方法将特定内容放入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">×</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>
这意味着模式应足够智能,以了解单击了哪个项目(Text1,Text2或Text3)并显示其存储在属性数据内容中的内容。
我该怎么做?
答案 0 :(得分:2)
模态正文将是已点击的data-content
元素的li
属性值。
请查看下面的代码段。
$("li[data-target='#myModal']").on('click',function(){
$(".modal-body").html($(this).attr('data-content'));
});
&#13;
<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">×</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>
&#13;