热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

jquery只显示1个活动元素-jquerydisplayonly1elementactive

Icreatedthiscode,clickonapictureandyoullcomeoutthedescriptionbelowtheimage.Iwould

I created this code, click on a picture and you'll come out the description below the image. I would like to improve this code, so I want to be active only one description of a time, so if I click on a image see the description of that just clicked, and hide the last activated. maybe with the code is more clear http://codepen.io/mp1985/pen/qOrpQX

我创建了这段代码,点击图片然后你就可以看到图片下方的描述了。我想改进这个代码,所以我想只激活一个时间的描述,所以如果我点击一个图像,看到刚刚点击的描述,并隐藏最后激活的。也许用代码更清晰http://codepen.io/mp1985/pen/qOrpQX

$( ".spec").click(function() {
    $(this).find(".image, .details-spec").toggle();
    $(this).find(".block-content").toggleClass('white'); 
 });

I tried with toggle(), toggleClass() and not() but without success. any idea? thanks

我尝试使用toggle(),toggleClass()而不是()但没有成功。任何想法?谢谢

3 个解决方案

#1


3  

You can use not() here to avoid clicked element from selector

你可以在这里使用not()来避免选择器中的clicked元素

var $spec = $(".spec").click(function() {
// caching selector $(".spec") for future use
  $spec
    .not(this)
    //  avoiding clicked element
    .find(".image")
    // getting image selector
    .show()
    // showing back image
    .end()
    // back to previous selector
    .find(".details-spec")
    // getting details
    .hide()
    // hiding it
    .end()
    // back to previous selector
    .find(".block-content")
    //  getting block content
    .removeClass('white');
  // removing class white
  $(this)
    .find(".image, .details-spec")
    // getting elements by class
    .toggle()
    // toggling visibility
    .end()
    // back to previous selector  
    .find(".block-content")
    // getting block content    
    .toggleClass('white');
  // toggling class  white
});
.block {
  position: relative;
  height: 300px;
  width: 100%;
  overflow: hidden;
  background: #f9bda1;
  margin-bottom: 1em;
}
.one-thirds > .block {
  background-color: #484343;
  cursor: pointer;
}
.block .image {
  background-position: center;
  background-repeat: no-repeat;
  background-size: cover;
}
.one-thirds {
  width: 32%;
  float: left;
  margin-right: 1%;
}
.full {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
}
h3 {
  font-size: 20px;
}
.details-spec {
  display: none;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 100%;
}
.white {
  color: white;
}
.active > .image {
  visibility: hidden;
}
.active .details-spec {
  display: block;
}

Title:

Lorem Lorem Lorem Lorem LoremLorem Lorem Lorem Lorem Lorem LoremLorem

Title:

Lorem Lorem Lorem Lorem LoremLorem Lorem Lorem Lorem Lorem LoremLorem

Title:

Lorem Lorem Lorem Lorem LoremLorem

#2


2  

You don't need that over complex jQuery. Set the styles with css and toggle a single class on the root element.

你不需要复杂的jQuery。使用css设置样式并在根元素上切换单个类。

CSS

.spec.active .image {
  display: none;
}
.spec.active .details-spec {
  display: block;
}
.spec.active .block-content {
  color: white;
}

Javascript

var $spec = $('.spec');

$spec.click(function() {
  $spec.not($(this)).removeClass('active');
  $(this).toggleClass('active');
});

DEMO: http://codepen.io/anon/pen/VvpXXe

#3


1  

First close all ($spec). Second open current.($this)

首先关闭所有($ spec)。第二个开路电流。($ this)

var $spec = $(".spec").click(function() {
    $spec.find(".image, .details-spec").show();
  $spec.find(".block-content").removeClass('white');
  $(this).find(".image, .details-spec").hide();
  $(this).find(".block-content").addClass('white');
});

推荐阅读
author-avatar
幽咽小香
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有