如何从JSON Flickr API加载图像到Bootstrap Modal
How to load image into Bootstrap Modal from JSON Flickr API
下面是我正在工作的代码作为一个例子。当我点击Flickr feed中的每张图片时,我希望能够加载Bootstrap Modal(它确实如此),但在Modal中显示特定的照片(它目前没有)。我尝试过很多不同的东西。但我无法让它工作。示例:http://jahax.com/ins/
<div class="container">
<h2 style="text-align: center;">Demonstration</h2>
<p class="text-center">Demo of Bootstrap, jQuery & JSON API</p>
<div class="row text-center">
</div>
<div id="images" class="row text-center"> </div>
</div>
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="nyModalLabel" aria-hidden="true">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title" id="myModalLabel">Demo of Modal</h4>
</div>
<div class="modal-body">
<img id="mimg" src="">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<script src="https://code.jquery.com/jquery.js"></script>
<script src="js/bootstrap.min.js"></script>
<script type="text/javascript">
// Start jQuery Function
$(document).ready(function(){
// JSON API to access Flickr
$.getJSON("http://api.flickr.com/services/feeds/photos_public.gne?tags=winter&format=json&jsoncallback=?", displayImages);
function displayImages(data) {
var iCount = 0;
var htmlString = "<div class=row>";
$.each(data.items, function(i,item){
if (iCount < 18) {
var sourceSquare = (item.media.m).replace("_m.jpg", "_q.jpg");
htmlString += '<a class="btn" data-toggle="modal" data-target="#myModal">';
htmlString += '<img src="' + sourceSquare + '">';
htmlString += '</a>';
}
iCount++;
});
// HTML into #images DIV
$('#images').html(htmlString + "</div>");
// Close down the JSON function call
}
// End jQuery function
});
</script>
<script type="text/javascript">
// Send Content to Bootstrap Modal
$('img').on('click',function()
{
var sr=$(this).attr('src');
$('#mimg').attr('src',sr);
$('#myModal').modal('show');
});
</script>
修改你的第二个脚本:
<script type="text/javascript">
// Send Content to Bootstrap Modal
$(document).on('click', 'img', function(){
var sr=$(this).attr('src');
$('#mimg').attr('src',sr);
$('#myModal').modal('show');
});
</script>
相关文章:
- Javascript+Bootstrap图像库未加载
- Bootstrap多选-在复选框后添加图像
- 用于图像上映射区域的Bootstrap Popover/Tooltip
- 正在Bootstrap中设置页眉的全宽背景图像
- Bootstrap 3图像自动缩放全屏和滚动效果
- 需要在bootstrap css中单击时将图像加载为模态
- 如何使用来自 Twitter Bootstrap 的弹出框来显示图像
- JQuery 和 Bootstrap:如何将图像列表中的图像添加到轮播
- 如何使用 Bootstrap 3 在悬停时创建滑动图像
- Bootstrap 3不允许我在悬停时使用CSS/JQuery图像替换
- Bootstrap 3旋转木马滑动时隐藏图像
- 如何获取由Bootstrap File Input插件创建的图像数据
- Netflix风格的图像连续旋转木马,鼠标悬停,支持Bootstrap
- Twitter Bootstrap 3中的图像默认情况下没有响应
- Bootstrap'中的滑动图像;旋转木马挨着
- 将图像和时尚文本传递到Bootstrap模态
- 正在加载Magento中产品图像的Bootstrap Lightbox
- Bootstrap旋转木马可在窗口调整大小时响应多个图像
- 如何使用bootstrap在mouseover上制作动态切换图像
- bootstrap/JS/Ajax按钮点击后获取表单以更改图像