使用btn-group切换图像集

时间:2016-02-02 16:17:01

标签: javascript jquery ruby-on-rails twitter-bootstrap

我不想一次显示所有图像,因为它只是混乱了页面设计。我想单击bootstrap btn-group中的按钮,然后显示相应的图像。不是javascript / jquery的家伙,所以我需要一些帮助。

  <div class="btn-group" role="group" aria-label="...">
    <button type="button" class="btn btn-default">Front</button>
    <button type="button" class="btn btn-default">Back</button>
    <button type="button" class="btn btn-default">Left</button>
    <button type="button" class="btn btn-default">Right</button>
  </div>

  <div class="front-product-img">
    <% front = @product['colors'][0]['images'].find{|img| img['label'] == 'Front'} %>
    <%= image_tag front['url'], class: 'img-responsive' if front.present? %>
  </div>

  <div class="back-product-img">
    <% back = @product['colors'][0]['images'].find{|img| img['label'] == 'Back'} %>
    <%= image_tag back['url'], class: 'img-responsive' if back.present? %>
  </div>

  <div class="left-product-img">
    <% left = @product['colors'][0]['images'].find{|img| img['label'] == 'Left'} %>
    <%= image_tag left['url'], class: 'img-responsive' if left.present? %>
  </div>

  <div class="right-product-img">
    <% right = @product['colors'][0]['images'].find{|img| img['label'] == 'Right'} %>
    <%= image_tag right['url'], class: 'img-responsive' if right.present? %>
  </div>

1 个答案:

答案 0 :(得分:0)

$(".btn-group[role='group'] button[type=button]").click(function() {
$("."+$(this).text().toLowerCase()+"-product-img").toggle();
});

根据您的要求使用上面的代码..

相关问题