Hide/Show jQuery 单击多个元素

Hide/Show many elements on single click by jQuery

我为 Hide/Show 中的许多元素编写了 jQuery 的几行代码,单击即可运行。但问题是;我有更多图像 class 项目,所以我的脚本会很长,我的问题是如何简化或缩短我的脚本,我的意思是任何替代方案或任何新想法?请推荐.

HTML:

<div id="choose-color">
<span>
 <i class="images-red" style="">Red Image</i>
  <i class="images-blue" style="display: none;">Blue Image</i>
   <i class="images-pink" style="display: none;">Pink Image</i>
 <!-- many many images -->
</span>

 <button class="red">Red</button>
  <button class="blue">Blue</button>
   <button class="pink">Pink</button>

</div>

JS: live demo >

$("button.red").click(function(){
    $(".images-red").show();
    $(".images-blue, .images-pink").hide();
});
$("button.blue").click(function(){
    $(".images-red, .images-pink").hide();
    $(".images-blue").show();
});
$("button.pink").click(function(){
    $(".images-red, .images-blue").hide();
    $(".images-pink").show();
});

请建议我的脚本的简短代码。谢谢。

您可以通过向这些按钮添加一个普通的 class 来实现,

var iTags = $("#choose-color span i");
$("#choose-color button.button").click(function(){
    iTags.hide().eq($(this).index("button.button")).show();
});

代码背后的概念是使用通用的 class 为按钮绑定点击事件。现在在事件处理程序中,隐藏所有已经缓存的 i 元素并显示与单击按钮具有相同索引的元素。

DEMO

更多详情:.eq() and .index(selector)

如果您的元素顺序不同,i 和按钮的顺序都不相同。然后你可以使用 javascript 的 dataset 功能来解决这个问题。

var iTags = $("#choose-color span i");
$("#choose-color button.button").click(function(){
    iTags.hide().filter(".images-" + this.dataset.class).show()
});

要实现这一点,您必须向按钮添加数据属性,例如,

<button data-class="red" class="button red">Red</button>

DEMO

这个有效

  $("#choose-color button").click(function(){
        var _class = $(this).attr('class');
        $("#choose-color i").hide();
        $(".images-"+_class).show();
    });

https://jsfiddle.net/455k1hhh/5/

我知道这可能不是最好的解决方案,但它应该可以完成工作。

$("button").click(function(){
    var classname = $(this).attr('class');
    $("#choose-color span i").hide();
    $(".images-"+classname).show();
});

您可以使用 data-* 属性执行以下操作,因为当您有更多相同颜色的元素时,使用按钮的索引将不起作用。如果您以后必须向按钮添加更多 类,则仅使用整个 class 属性将不起作用。

$("button").click(function() {
  var color = $(this).data('color');
  var targets = $('.images-' + color);
  targets.show();
  $("span i").not(targets).hide();
});
.hidden {
  display: none
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<br/>
<br/>
<div id="choose-color">
  <span>
<i class="images-red">Red Image</i>
<i class="images-blue hidden">Blue Image</i>
<i class="images-pink hidden">Pink Image</i>
<!-- Many many image -->
</span>
  <br/>
  <br/>
  <button data-color="red">Red</button>
  <button data-color="blue">Blue</button>
  <button data-color="pink">Pink</button>

</div>

让所有图像共享一个 class(例如 .image)是有意义的。然后,您只需为按钮和图像使用共享的 class;在这个例子中,我使用了颜色名称。现在,当单击任何按钮时,您可以获取要显示的图像的 class 名称。

试一试:

$("button").click(function(){
    $(".image").hide();
    var className = $(this).attr("class");
    $("." + className).show();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<br/><br/>
<div id="choose-color">
<span>
<i class="image red" style="">Red Image</i>
<i class="image blue" style="display: none;">Blue Image</i>
<i class="image pink" style="display: none;">Pink Image</i>
<!-- Many many image -->
</span>
<br/><br/>
<button class="red">Red</button>
<button class="blue">Blue</button>
<button class="pink">Pink</button>

</div>

由于依赖 class 名称,您通过这种方式使未来的可扩展性有点困难,但这将解决您的直接需求:

<div id="myImages">
  <i class="images-red" style="">Red Image</i>
  <i class="images-blue" style="display: none;">Blue Image</i>
  <i class="images-pink" style="display: none;">Pink Image</i>
  <!-- Many many image -->
</div>
<div id="myButtons">
  <button class="red">Red</button>
  <button class="blue">Blue</button>
  <button class="pink">Pink</button>
</div>
$("#myButtons button").click(function(){
        var color = $(this).attr("class");
    var imageClass = ".images-"+color;
    $('#myImages').children("i").each(function () {
        $(this).hide();
    });
    $(imageClass).show();
});

这是一个JSFiddle

编辑: 请注意我是如何将按钮和图像包裹在父 div 中的,以便您可以仅隔离要使用的 buttons/images。

你可以试试这个:

<div id="choose-color">
<span>
<i class="images-red" style="">Red Image</i>
<i class="images-blue" style="display: none;">Blue Image</i>
<i class="images-pink" style="display: none;">Pink Image</i>
<!-- Many image -->
</span>
<br/><br/>
<button class="colour red" onclick="myFunction(this)">Red</button>
<button class="colour blue" onclick="myFunction(this)">Blue</button>
<button class="colour pink" onclick="myFunction(this)">Pink</button>

</div>

JS: see here

$(".colour").click(function(){
    var colors = ["red", "blue", "pink"];
    for (i = 0; i < colors.length; i++) {
        if($(this).hasClass(colors[i])){
            $(".images-"+colors[i]).show();
        }else{
            $(".images-"+colors[i]).hide();
        }
    }
});