如何在点击时更改CSS?

时间:2018-09-12 15:07:07

标签: javascript jquery html css html5

我有4张包含数据的卡。我想要单击第一个标题文本卡时,它向上扩展,并显示我的信息,当单击同一张卡时,它会隐藏正文,但其余卡保持不变,

这是我想要的东西。

enter image description here

  

更新

这是JSFiddle,它具有所有四张卡:http://jsfiddle.net/Mwanitete/rzjxkv8e/2/ 这是HTML:

<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js"></script>

<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>

<div class="data">

<div class="card">
    <div class="card-header">
        <a data-toggle="collapse" href="#test-block" aria-expanded="true" aria-controls="test-block">
            card header
        </a>
    </div>
    <div id="test-block" class="collapse">
        <div class="card-body">
            card block
        </div>
    </div>
</div>

<div class="card">
    <div class="card-header">
        <a data-toggle="collapse" href="#test-block" aria-expanded="true" aria-controls="test-block">
            card header
        </a>
    </div>
    <div id="test-block" class="collapse">
        <div class="card-body">
            card block
        </div>
    </div>
</div>
<div class="card">
    <div class="card-header">
        <a data-toggle="collapse" href="#test-block" aria-expanded="true" aria-controls="test-block">
            card header
        </a>
    </div>
    <div id="test-block" class="collapse">
        <div class="card-body">
            card block
        </div>
    </div>
</div>
<div class="card">
    <div class="card-header">
        <a data-toggle="collapse" href="#test-block" aria-expanded="true" aria-controls="test-block">
            card header
        </a>
    </div>
    <div id="test-block" class="collapse">
        <div class="card-body">
            card block
        </div>
    </div>
</div>
</div>

这是JavaScript

$('.card-header').click(function(){
    $('.card').toggleClass('.card-size')
})

这是CSS

.data{
  display: flex;
}

.card-size{
  height: 224px;
}

3 个答案:

答案 0 :(得分:3)

这是一个具有可行解决方案的JSFiddle。

我已经删除了折叠属性,并将其拉到准系统。

http://jsfiddle.net/9musLxy4/88/

HTML:

<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js"></script>

<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>

<div class="data">

<div class='card-container' data-card-id='1'>
  <div class='card-header' data-card-id='1'>
    Header
  </div>
  <div class='card-body collapse' data-card-id='1'>
    <div class='data'>
      Body 1
    </div>
  </div>
</div>

<div class='card-container' data-card-id='2'>
  <div class='card-header' data-card-id='2'>
    Header
  </div>
  <div class='card-body collapse' data-card-id='2'>
    <div class='data'>
      Body 2
    </div>
  </div>
</div>

<div class='card-container' data-card-id='3'>
  <div class='card-header' data-card-id='3'>
    Header
  </div>
  <div class='card-body collapse' data-card-id='3'>
    <div class='data'>
      Body 3
    </div>
  </div>
</div>

<div class='card-container' data-card-id='4'>
  <div class='card-header' data-card-id='4'>
    Header
  </div>
  <div class='card-body collapse' data-card-id='4'>
    <div class='data'>
      Body 4
    </div>
  </div>
</div>
</div>

CSS:

.data{
  display: flex;
}

.card-body {
  border: 1px solid black;
}

JS:

$('.card-container').on('click', function(event) {
    var id = $(this).data('card-id')
    $('.card-body[data-card-id="' + id + '"]').toggle()
})

答案 1 :(得分:0)

$('.card-header').click(function(){
    $(this).toggleClass('.card-size');
    $(this).parent().find('#test-block').toggleClass('show')
})

使用此

答案 2 :(得分:0)

您是否只是在尝试扩展卡的大小?如果是这样,则丢失JQuery,您只需要一种CSS样式:

map.containsKey()

如果要在打开卡片后将其大小保持不变,则不想切换大小类别,只需添加即可。

#test-block .card-body {
  height: 224px;
}

http://jsfiddle.net/9musLxy4/74/