如何使用javascript将图像加载到bootstrap模式中 [英] How to load image into bootstrap modal with javascript

查看:61
本文介绍了如何使用javascript将图像加载到bootstrap模式中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的设置是4个链接(如下)。我希望每个链接都打开myModal,但是根据点击的链接,应该在模式中加载不同的图像文件。我得到了其中一个链接。

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.

<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?

推荐答案

通过使用jquery .click ,您可以轻松实现此目标(使用html5数据)属性也是如此。

By using jquery .click you can easily achieve this (with html5 data attribute too).

<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>

脚本:

$('li a').click(function (e) {
    $('#myModal img').attr('src', $(this).attr('data-img-url'));
});

小提琴: http://jsfiddle.net/vLSWH/292/

注意:虽然你没有指定使用jQuery,但twitter-bootstrap模态要求你使用jQuery,所以不妨使用它。

Note: Though you did not specify using jQuery, twitter-bootstrap modal require you to use jQuery anyway, so might as well use it.

这篇关于如何使用javascript将图像加载到bootstrap模式中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆