javascript - 为单个父单击显示/隐藏多个子类(Jquery)

标签 javascript jquery html css jquery-ui

我是 Javascript/Jquery 的新手,我试图在单击特定父类时隐藏多个子类/相邻类。

这是我的 HTML

<div class="container">
  <div class="row">
    <div class ="col-md-2 pov_icon">
     <div class="pov_icon_small">
      <i class="fa fa-clock-o"></i>
     </div>
     <div class="pov_title_small">
       MEASURE
     </div>
    </div>

    <div class ="col-md-2 pov_icon">
     <div class="pov_icon_large">
      <i class="fa fa-map-marker"></i>
     </div>
     <div class="pov_title_large">
       MEASURE
     </div>
    </div>

    <div class ="col-md-2 pov_icon">
     <div class="pov_icon_small">
      <i class="fa fa-commenting"></i>
     </div>
     <div class="pov_title_small">
       MEASURE
     </div>
    </div>
</div>
</div>

我的目标是:当用户单击显示的两个较小图标之一 (pov_icon_small) 时,对于该单独的图标:类 pov_icon_smallpov_title_small 将分别更改为 pov_icon_largepov_title_large。同时,我希望其他“大”图标和“标题”恢复到“小”状态

我已经开始调用一些 Javascript,但我认为我的方向不对:

$('.pov_icon_small').on('click', function (e) {
    $(this).toggleClass("pov_icon_large");
});

有人愿意为我指出正确的方向吗?

最佳答案

使用单次点击

$('.pov_icon_small , .pov_icon_large').on('click', function (e) {
    $('.pov_icon_large').not($(this)).removeClass('pov_icon_large').addClass('pov_icon_small');
    $(this).toggleClass("pov_icon_small").toggleClass("pov_icon_large");
});

和标题一样

$('.pov_title_small , .pov_title_large').on('click', function (e) {
    $('.pov_title_large').not($(this)).removeClass('pov_title_large').addClass('pov_title_small'); 
    $(this).toggleClass("pov_title_small").toggleClass("pov_title_large");
});

Working Demo

要在图标点击时运行这两个操作,请使用此

$('.pov_icon_small , .pov_icon_large').on('click', function () {
    $('.pov_icon_large').not($(this)).removeClass('pov_icon_large').addClass('pov_icon_small');
    $('.pov_title_large').not($(this).next('div[class^="pov_title_"]')).removeClass('pov_title_large').addClass('pov_title_small'); 
    $(this).toggleClass("pov_icon_small").toggleClass("pov_icon_large");
    $(this).next('div[class^="pov_title_"]').toggleClass("pov_title_small").toggleClass("pov_title_large");
});

Working Demo

Note: be sure to include Jquery

关于javascript - 为单个父单击显示/隐藏多个子类(Jquery),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34279753/

相关文章:

javascript - jQuery No-Ui-Slider,如何锁定多个互连的 slider 总和为 100?

jquery验证: how can I make a field validate on change?

javascript - 如何模拟点击事件

javascript - 通过链接替换 ​​div 类

html - 如何在具有不同大小的子 div 的父 div 中居中文本(flexbox)

html - 不透明度仅在 ul 上不在 li css 上

javascript - 正则表达式基于字符串值匹配对象键,该字符串值可以是精确的或包含键

javascript - Chai 深平等和平等不工作

javascript - 结束游览一次后通过按钮启动 Bootstrap 游览

javascript - 动态创建 div 容器并将它们添加到现有的 div 中