【问题标题】:Modal images return undefined for ruby/rails generated images, works for static images对于 ruby​​/rails 生成的图像,模态图像返回 undefined,适用于静态图像
【发布时间】:2020-04-20 09:17:43
【问题描述】:

你好 stackoverflow 社区。​​p>

我正在尝试将多个图像模式添加到带有图像的简单 Rails 博客文章中。

我使用带有简单posts.each 循环的帖子生成器来生成帖子。 然而,当使用 javascript 创建图像模态时,.each 循环生成的图像只是在模态中返回 404 image not found。 为了测试,我链接了一些静态图片,并且里面的图片成功创建了模态。

我认为这与在 ruby​​ 代码之前执行的 javascript 有关,但是我不确定。

//RUBY CODE to generate posts
        <div class="projectcolumn">
      <% @posts.each do |post| %>
        <div class="blurbox">
          <h1><%= post.title %></h1>
          <p><%= post.content %></p>
          <div class="postimage" id="myImg">
            <%= image_tag(post.image)%>
          </div>
        </div>

      <% end %>

//static images I used to test if the modal is working    
      <img class="postimage" id="myImg" src="http://onebigphoto.com/uploads/2012/10/midnight-sun-in-lofoten-norway.jpg" alt="Midnight sun in Lofoten, Norway" width="300" height="200">
    <img class="postimage" id="myImg" src="http://cdn-image.travelandleisure.com/sites/default/files/styles/1600x1000/public/1490029386/fisherman-cabin-hamnoy-lofoten-islands-norway-NORWAY0320.jpg?itok=cpPuUjh1" alt="Fishermen's cabins in Lofoten, Norway"
    width="300" height="200">
    <img class="postimage" id="myImg" src="http://fjordtours.blob.core.windows.net/fjordtours-umbraco/1199/gerirangerfjord-per-ottar-walderhaug-fjordnorway.jpg" alt="Gerirangerfjord, Norway" width="300" height="200">


//the modal div
    <div id="myModal" class="modal">
      <span class="close">&times;</span>
      <img class="modal-content" id="img01">
      <div id="caption"></div>
    </div>
    </div>

    <script>// create references to the modal...
    var modal = document.getElementById('myModal');
    // to all images -- note I'm using a class!
    var images = document.getElementsByClassName('postimage');
    // the image in the modal
    var modalImg = document.getElementById("img01");
    // and the caption in the modal
    var captionText = document.getElementById("caption");

    // Go through all of the images with our custom class
    for (var i = 0; i < images.length; i++) {
      var img = images[i];
      // and attach our click listener for this image.
      img.onclick = function(evt) {
        modal.style.display = "block";
        modalImg.src = this.src;
        captionText.innerHTML = this.alt;
      }
    }

    var span = document.getElementsByClassName("close")[0];

    span.onclick = function() {
      modal.style.display = "none";
    }
    </script>

我认为通过将脚本标签放在 index.html.erb 文件的末尾,javascript 将在图像全部构建后编译。这种思路有缺陷吗?

【问题讨论】:

    标签: javascript ruby-on-rails image modal-dialog simplemodal


    【解决方案1】:

    另一个网站上的一位乐于助人的用户为我解决了这个问题。

    javascript

    <script>
      document.addEventListener("DOMContentLoaded", modalMaker);
    
      function modalMaker() {
    
        // Get the modal
      var modal = document.getElementById('myModal');
    
      // Get the image and insert it inside the modal - use its "alt" text as a caption
      var img = $('.postimage');
      var modalImg = $("#img01");
      var captionText = document.getElementById("caption");
      $('.postimage').click(function(){
          modal.style.display = "block";
          var imageSrc = $(this).find("img").attr("src");
          var imageAlt = $(this).find("img").attr("alt");
          modalImg.attr('src', imageSrc);
          captionText.innerHTML = imageAlt;
      });    
      // Get the <span> element that closes the modal
      var span = document.getElementsByClassName("close")[0];
    
      // When the user clicks on <span> (x), close the modal
      span.onclick = function() {
        modal.style.display = "none";
      }
    
    
      }
    </script>
    

    ruby on rails / html 代码

    <div class="projectcolumn">
      <% @posts.each do |post| %>
        <div class="blurbox">
          <h1><%= post.title %></h1>
          <p><%= post.content %></p>
          <div class="postimage">
            <%= image_tag(post.image, alt: "caption")%>
          </div>
        </div>
      <% end %>
      <!-- The Modal -->
      <div id="myModal" class="modal">
        <!-- The Close Button -->
        <span class="close" onclick="document.getElementById('myModal').style.display='none'">&times;</span>
        <!-- Modal Content (The Image) -->
        <img class="modal-content" id="img01">
        <!-- Modal Caption (Image Text) -->
        <div id="caption"></div>
      </div>
    </div>
    

    CSS

    /* Modal Content (Image) */
    .modal-content {
      margin: auto;
      display: block;
      width: 80%;
      max-width: 700px;
    }
    
    /* Caption of Modal Image (Image Text) - Same Width as the Image */
    #caption {
      margin: auto;
      display: block;
      width: 80%;
      max-width: 700px;
      text-align: center;
      color: #ccc;
      padding: 10px 0;
      height: 150px;
    }
    
    /* Add Animation - Zoom in the Modal */
    .modal-content, #caption {
      -webkit-animation-name: zoom;
      -webkit-animation-duration: 0.6s;
      animation-name: zoom;
      animation-duration: 0.6s;
    }
    
    @-webkit-keyframes zoom {
      from {-webkit-transform:scale(0)}
      to {-webkit-transform:scale(1)}
    }
    
    @keyframes zoom {
      from {transform:scale(0)}
      to {transform:scale(1)}
    }
    
    /* The Close Button */
    .close {
      position: absolute;
      top: 15px;
      right: 35px;
      color: #f1f1f1;
      font-size: 40px;
      font-weight: bold;
      transition: 0.3s;
    }
    
    .close:hover,
    .close:focus {
      color: #bbb;
      text-decoration: none;
      cursor: pointer;
    }
    
    /* 100% Image Width on Smaller Screens */
    @media only screen and (max-width: 700px){
      .modal-content {
          width: 100%;
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-02
      • 1970-01-01
      • 1970-01-01
      • 2015-08-08
      • 1970-01-01
      • 2017-02-24
      • 2021-06-18
      • 2015-09-25
      相关资源
      最近更新 更多