javascript - 如何为带有图片的单选按钮制作显示和隐藏标题

标签 javascript jquery html

我的设计是一个带有图片的单选按钮。我想添加显示和隐藏描述效果。单击单选按钮后,应显示某个描述,如果单击另一个单选按钮,则应隐藏先前的描述,然后显示当前按钮的描述。

这是我的带有图片的单选按钮的 html 代码:

` 我们的成分...

<div class="slider">
<!-- Lamb Kebab Pizza -->
<input type="radio" name="slide_switch" value="kebab" id="id1" checked="checked"/>
    <label for="id1">
    <img src="../img/kebab.jpg" width="100" alt="Lamb Kebab Pizza" class="img img-responsive" />
    </label>
<img src="../img/kebab.jpg" class="img img-responsive" />


<!-- Fennel Sausage Pizza -->
<input type="radio" name="slide_switch" value="fennel" id="id2" />
<label for="id2">
<img src="../img/fennel_copy.jpg" width="100" alt="Fennel Sausage Pizza" class="img img-responsive" />
</label>
<img src="../img/fennel_copy.jpg" class="img img-responsive" />


<!-- Pistachio and Sausage Pizza -->
<input type="radio" name="slide_switch" value="pistachio" id="id3" />
<label for="id3">
<img src="../img/pistachio.jpg" width="100" alt="Pistachio and Sausage Pizza" class="img img-responsive" />
</label>
<img src="../img/pistachio.jpg" class="img img-responsive" />


<!-- White Truffle Pizza -->
<input type="radio" name="slide_switch" value="white" id="id4" />
<label for="id4">
<img src="../img/white_truffle.jpg" width="100" alt="White Truffle Pizza" class="img img-responsive" />
</label>
<img src="../img/white_truffle.jpg" class="img img-responsive" />


<!-- Surly Pizza -->
<input type="radio" name="slide_switch" value="surly" id="id5" />
<label for="id5">
<img src="../img/surly_pizza.jpg" width="100" alt="Surly Pizza" class="img img-responsive" />
</label>
<img src="../img/surly_pizza.jpg" class="img img-responsive" />

</div>
<!-- Lamb Kebab Pizza -->
<div class="kebab">Kebab</div>

<!-- Fennel Sausage Pizza -->
<div class="fennel">Fennel</div>

<!-- Pistachio and Sausage Pizza -->
<div class="pistachio">Pistachio</div>

<!-- White Truffle Pizza -->
<div class="white">White</div>

<!-- Surly Pizza -->
<div class="surly">Surly</div>

`

我的 JavaScript 代码是:

$(document).ready(function(){
    $("input[name$='slide_switch']").click(function() {
    var value = $(this).val();

    if (value == 'fennel') {
      $(".fennel").show();
      $(".kebab",".pistachio",".white",".surly").hide();
    }
    else if (value == 'pistachio') {
      $(".pistachio").show();
      $(".kebab",".fennel",".pistachio",".surly").hide();
    }
    else if (value == 'white') {
      $(".white").show();
      $(".kebab",".fennel",".pistachio",".surly").hide();
    }
    else if (value == 'surly') {
      $(".surly").show();
      $(".kebab",".fennel",".pistachio","white").hide();
    }
    });
    $(".kebab").show();
    $(".fennel",".pistachio",".white",".surly").hide();
});

最佳答案

有这样的事吗?

$("input[name='slide_switch']").click(function () {
    $('img').hide();
    $('[src*="'+this.value+'"]').show();
    $('.common').hide();
    $('.' + this.value).show();
});

注意:我向元素添加了一个公共(public)类,以稍微缩短代码。

DEMO

关于javascript - 如何为带有图片的单选按钮制作显示和隐藏标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25182713/

相关文章:

javascript - 将 Javascript 字符串转换为日期

javascript - 将数据添加到表中并将其保存到数据库中

javascript - 如何在 Underscore.js 中使用 _.each() 遍历每 2 个项目

html - CSS 使用 100% 的高度和宽度制作 App

php - 使用默认样式值编辑段落标签

javascript - 使用地理位置更改货币

javascript - PCI 合规性 - 脚本源完整性检查

jquery - 在表单输入焦点上,显示 div

javascript - 如何使用 javascript 访问数组中的类并设置它们的样式

html - 我应该如何在 HTML 中表达像 15/16ths 这样的分数?