简体   繁体   English

如何从JSON Flickr API将图像加载到Bootstrap Modal中

[英]How to load image into Bootstrap Modal from JSON Flickr API

Below is the code I am working with as an example. 以下是我正在使用的代码作为示例。 When I click each image from the Flickr feed I want to be able to load the Bootstrap Modal (which it does) but display that particular photo in the Modal (which it currently doesnt). 当我从Flickr提要中单击每个图像时,我希望能够加载Bootstrap Modal(它确实如此),但在Modal中显示该特定照片(当前它没有)。 I've tried numerous different things. 我尝试了许多不同的事情。 But I am unable to get it to work. 但是我无法使其正常工作。 Example at http://jahax.com/ins/ 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">&times;</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>

Change your 2nd script to this: 将您的第二个脚本更改为此:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM