javascript - 如何使用 Bootstrap 将图像显示为弹出窗口

标签 javascript jquery css twitter-bootstrap

我想做与下面 Fiddle 中发生的相同的事情

http://jsfiddle.net/6CR2H/1/

我还是这样做了

<link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet">

<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>

<a href="#" class="pop">
    <img src="http://patyshibuya.com.br/wp-content/uploads/2014/04/04.jpg" style="width: 400px; height: 264px;">
</a>

<a href="#" class="pop">
    <img src="http://upload.wikimedia.org/wikipedia/commons/2/22/Turkish_Van_Cat.jpg" style="width: 400px; height: 264px;">
</a>

<div class="modal fade" id="imagemodal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">              
      <div class="modal-body">
        <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
        <img src="" class="imagepreview" style="width: 100%;" >
      </div>
    </div>
  </div>
</div>

<script>
$(function() {
        $('.pop').on('click', function() {
            $('.imagepreview').attr('src', $(this).find('img').attr('src'));
            $('#imagemodal').modal('show');   
        });     
});
</script>

但它不起作用,我还包含了两个外部文件,但不知道为什么单击它后图像没有显示为弹出

请帮我找到问题

如果有人能帮助我,我将非常高兴。

谢谢

最佳答案

Bootstrap 需要 jQuery。

<link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet">
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>

<a href="#" class="pop">
  <img src="http://patyshibuya.com.br/wp-content/uploads/2014/04/04.jpg" style="width: 400px; height: 264px;">
</a>

<a href="#" class="pop">
  <img src="http://upload.wikimedia.org/wikipedia/commons/2/22/Turkish_Van_Cat.jpg" style="width: 400px; height: 264px;">
</a>

<div class="modal fade" id="imagemodal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-body">
        <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
        <img src="" class="imagepreview" style="width: 100%;">
      </div>
    </div>
  </div>
</div>

<script>
  $(function() {
          $('.pop').on('click', function() {
              $('.imagepreview').attr('src', $(this).find('img').attr('src'));
              $('#imagemodal').modal('show');   
          });     
  });
</script>

关于javascript - 如何使用 Bootstrap 将图像显示为弹出窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42569261/

相关文章:

javascript - 我正在尝试发布评论,工作了一秒钟,但一旦我单击提交按钮,它们就会消失

javascript - 如何在不使用 HTML 的情况下从另一个 javascript 文件调用一个 javascript 文件的函数

javascript - 当列数为 4 时,如何务实地让 ng-repeat 添加另一行?

javascript - 我的 JavaScript 打印代码有效但无法使用 css

javascript - jQuery GraphML 解析(清理导入的数据)

jquery 在元素中应用 rgb 边框

jquery - 如何使用 ajax 发布包含动态表中的字段的表单,可以为其动态添加行和列

jquery - 我如何使用 AJAX 使我的登录和注册页面在 Rails 3 中相同?

transform - CSS3 Perspective、Transform 和 Transition 使用 jQuery 改变

css - 跳过第一个关键帧动画