dongshan8953 2018-07-09 19:43
浏览 331

如何将预加载器添加到其src动态变化的iframe中?

I'm using an iframe inside of bootstrap modal:

<div class="modal fade" id="newsModal" tabindex="-1" role="dialog" aria-labelledby="newsModal" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-body">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
        <div>
          <div id="loadingMessage">Loading...</div> 
          <iframe width="100%" class="frame embed-responsive-item" height="350" src=""></iframe>
        </div>
      </div>
    </div>
  </div>
</div>

That iframe is open when an element of some elements with the same class clicked:

<?php foreach($new as $news){ ?>
    <div class="post_frame" data-link="<?php echo $news['link']; ?>" data-toggle="modal" data-target="#newsModal">
         Lorem ipsum dolor sit amet
    </div>
<?php } ?>

I'm using that code to set the source of the iframe:

$('.post_frame').each(function() {
    $(this).click(function() {
        var data = $(this).data('link'),
            frame = $('.frame');

        frame.attr('src',  data);
    });     

});

To hide the preloader I use:

frame.load(function() {
    $('#loadingMessage').css('display', 'none');
});

But the preloader still there.

And when I close that modal and click on another element with the same class post_frame, The previous source content still there until the content of the new source loads.

How to create a preloader before each source is loaded?

  • 写回答

1条回答 默认 最新

  • duangan1945 2018-07-09 19:48
    关注

    Change this:

    var data = $(this).data('link'),

    To this:

    var data = $(this).data('link');

    And then remove this from where it currently is (inside function):

    frame = $('.frame');

    And place it in the global scope outside the function:

    var frame = $('.frame');

    评论

报告相同问题?

悬赏问题

  • ¥15 解决一个加好友限制问题 或者有好的方案
  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥30 求解达问题(有红包)