简体   繁体   English

更改图像而不刷新页面或重新加载

[英]Change image without page refresh or reload

I am using this guide. 我正在使用本指南。

http://jsfiddle.net/nz8qy41y/ http://jsfiddle.net/nz8qy41y/

Problem: When I click on [1] [2] or [3] main image is not changing. 问题: 当我单击[1] [2]或[3]时,主图像没有改变。 Instead I am redirected to image. 相反,我被重定向到图像。

View. 视图。 Main image: 主图:

<div class="block-2-image"> 

<a href=<%=@advertisement.pictures.first.image.url(:original)%> data-lightbox="gallery"><%=  image_tag @advertisement.pictures.first.image.url(:medium),
    :title=> @advertisement.name,:id=>"main-image", :alt =>                                   @advertisement.name%></a>

</div>

Thumbnail images: 缩略图:

<ul class="thumbnail">
    <% @advertisement.pictures.to_enum.with_index(1) do |thumb, index|%>


         <li><a href=<%=thumb.image.url(:medium)%>><%= index %></a></li>


    <% end %> 
</ul>

Script: 脚本:

$(document).ready(function(){
   $('.thumbnail li').on('click',function(){
      $('.block-2-image a').find('img').attr('src', $(this).attr('src'));
   });
});

Now when I click on small images it links to right image, but in other view. 现在,当我单击小图像时,它链接到正确的图像,但在其他视图中。 I need to change image in the same view. 我需要在同一视图中更改图像。

Any help ? 有什么帮助吗?

Thanks. 谢谢。

include jquery if it not included in jquery 如果不包含在jquery中,请包含jquery

$(document).ready(function(){
   $('.thumbnail li img').on('click',function(){
      $('.block-2-image').find('img').attr('src', $(this).attr('src'));
   });
});

but with that way its better to type a full path for your images in html 但是用这种方式最好在html中为图像键入完整路径

Pure JS variation of @Mohamed's answer (not tested): @Mohamed答案的纯JS变体(未经测试):

window.onload = function() {
    var selector = document.querySelector(".thumbnail li img");
    selector.addEventListener("click", function() {
        var img = document.querySelector(".block-2-image img");
        img.setAttribute("src", selector.getAttribute("src"));
    });
}

It was my mistake. 是我的错

I had duplicate script code inside Application.js that mixed up everything. 我在Application.js中有重复的脚本代码,这些脚本代码混合了所有内容。

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

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