twitter-bootstrap 如何使用javascript将图像加载到引导模式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18754900/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to load image into bootstrap modal with javascript
提问by Ben
My setup is 4 links (below). I want each link to open myModal, but depending on which link gets clicked, a different image file should load in the modal. I got it working for one of the links.
我的设置是 4 个链接(如下)。我希望每个链接都打开 myModal,但是根据点击的链接,应该在模态中加载不同的图像文件。我让它为其中一个链接工作。
<li><a href="#myModal" data-toggle="modal">6 Teams</a></li>
<li><a href="#">5 Teams</a></li>
<li><a href="#">4 Teams</a></li>
<li><a href="#">3 Teams</a></li>
<!-- Modal -->
<div id="myModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" style="width:800px;">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
</div>
<div class="modal-body">
<img src="/images/brackets/6teamDouble1.jpg">
</div>
<div class="modal-footer">
<button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
</div>
</div>
How do I get this to work for each link without building a separate modal for each one?
如何在不为每个链接构建单独的模式的情况下使其适用于每个链接?
回答by JofryHS
By using jquery .clickyou can easily achieve this (with html5 data attribute too).
通过使用 jquery,.click您可以轻松实现这一点(也使用 html5 数据属性)。
<ul>
<li><a href="#myModal" data-toggle="modal" data-img-url="http://placehold.it/200x200/dddddd/ffffff&text=Hey1">6 Teams</a>
</li>
<li><a href="#myModal" data-toggle="modal" data-img-url="http://placehold.it/200x200/dddddd/ffffff&text=Hey3">5 Teams</a>
</li>
<li><a href="#myModal" data-toggle="modal" data-img-url="http://placehold.it/200x200/dddddd/ffffff&text=Hey4">4 Teams</a>
</li>
<li><a href="#myModal" data-toggle="modal" data-img-url="http://placehold.it/200x200/dddddd/ffffff&text=Hey5">3 Teams</a>
</li>
</ul>
Script:
脚本:
$('li a').click(function (e) {
$('#myModal img').attr('src', $(this).attr('data-img-url'));
});
Fiddle: http://jsfiddle.net/vLSWH/292/
小提琴:http: //jsfiddle.net/vLSWH/292/
Note: Though you did not specify using jQuery, twitter-bootstrap modal require you to use jQuery anyway, so might as well use it.
注意:虽然您没有指定使用 jQuery,但 twitter-bootstrap modal 要求您无论如何都使用 jQuery,所以不妨使用它。

