每行只突出显示一个单元格

时间:2016-04-26 18:28:39

标签: javascript jquery html css

我的目标是能够在点击时突出显示单元格,并在再次单击时取消突出显示。

每行只能突出显示一个单元格。

以下是我尝试过的代码,这里是fiddle

感谢。

HTML:

<div class="hws-css-filter" >
  <div class="hws-css-filter-tr">
    <div class="hws-css-filter-td"></div>
    <div class="hws-css-filter-td"></div>
    <div class="hws-css-filter-td"></div>
  </div>
  <div class="hws-css-filter-tr">
    <div class="hws-css-filter-td"></div>
    <div class="hws-css-filter-td"></div>
    <div class="hws-css-filter-td"></div>
  </div>
</div>

的jQuery

$('.hws-css-filter-td').click(function () {
    var theTable = $(this).closest('.hws-css-filter-tr');
    $(theTable).each(function () {
        $(this).css("background-color", "");
    });
    $(this).css("background-color", "blue");
});

4 个答案:

答案 0 :(得分:4)

首先删除所有兄弟姐妹的背景颜色,如下所示。

&#13;
&#13;
$('.hws-css-filter-td').click(function() {
  $(this).siblings().css("background-color", "");
  $(this).css("background-color", "blue");
});
&#13;
.hws-css-filter {
  display: table;
  margin: auto;
  vertical-align: middle;
}

.hws-css-filter-tr {
  display: table-row;
  padding: 0;
  margin: 0;
}

.hws-css-filter-td {
  display: table-cell;
  border: 1px solid #000;
  width: 15px;
  height: 15px;
  line-height: 15px;
  text-align: center;
  vertical-align: middle;
  padding: 0;
  margin: 0;
  font-family: monospace;
  font-size: 10px;
  text-transform: uppercase;
  cursor: pointer;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="hws-css-filter">
  <div class="hws-css-filter-tr">
    <div class="hws-css-filter-td"></div>
    <div class="hws-css-filter-td"></div>
    <div class="hws-css-filter-td"></div>
    <div class="hws-css-filter-td"></div>
    <div class="hws-css-filter-td"></div>
  </div>
  <div class="hws-css-filter-tr">
    <div class="hws-css-filter-td"></div>
    <div class="hws-css-filter-td"></div>
    <div class="hws-css-filter-td"></div>
    <div class="hws-css-filter-td"></div>
    <div class="hws-css-filter-td"></div>
  </div>
  <div class="hws-css-filter-tr">
    <div class="hws-css-filter-td"></div>
    <div class="hws-css-filter-td"></div>
    <div class="hws-css-filter-td"></div>
    <div class="hws-css-filter-td"></div>
    <div class="hws-css-filter-td"></div>
  </div>
</div>
&#13;
&#13;
&#13;

更新:选择未选中。

&#13;
&#13;
$('.hws-css-filter-td').click(function() {
  $(this).siblings().removeClass('selected');
  $(this).toggleClass('selected');
});
&#13;
.hws-css-filter {
  display: table;
  margin: auto;
  vertical-align: middle;
}

.hws-css-filter-tr {
  display: table-row;
  padding: 0;
  margin: 0;
}

.hws-css-filter-td {
  display: table-cell;
  border: 1px solid #000;
  width: 15px;
  height: 15px;
  line-height: 15px;
  text-align: center;
  vertical-align: middle;
  padding: 0;
  margin: 0;
  font-family: monospace;
  font-size: 10px;
  text-transform: uppercase;
  cursor: pointer;
}

.selected {
  background-color: blue;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="hws-css-filter">
  <div class="hws-css-filter-tr">
    <div class="hws-css-filter-td"></div>
    <div class="hws-css-filter-td"></div>
    <div class="hws-css-filter-td"></div>
    <div class="hws-css-filter-td"></div>
    <div class="hws-css-filter-td"></div>
  </div>
  <div class="hws-css-filter-tr">
    <div class="hws-css-filter-td"></div>
    <div class="hws-css-filter-td"></div>
    <div class="hws-css-filter-td"></div>
    <div class="hws-css-filter-td"></div>
    <div class="hws-css-filter-td"></div>
  </div>
  <div class="hws-css-filter-tr">
    <div class="hws-css-filter-td"></div>
    <div class="hws-css-filter-td"></div>
    <div class="hws-css-filter-td"></div>
    <div class="hws-css-filter-td"></div>
    <div class="hws-css-filter-td"></div>
  </div>
</div>
&#13;
&#13;
&#13;

<强> UPDATED FIDDLE

答案 1 :(得分:1)

为了更好地管理单元格格式,请为突出显示的单元格创建新样式

.cell_highlight{
    background-color: blue;
}

并设置jQuery事件:

$('.hws-css-filter-td').click(function () {
    $(this).siblings().removeClass("cell_highlight"); 
    $(this).addClass("cell_highlight");
});

答案 2 :(得分:0)

这将满足您的所有要求:

  1. 点击
  2. 突出显示
  3. 如果再次点击则取消突出显示
  4. 每行只允许一个突出显示的单元格
  5. $('.hws-css-filter-td').on('click', function() {
      var $this = $(this);
      
      if ($this.hasClass('blue-highlight')) {
        $this.removeClass('blue-highlight');
      } else {
        $this.addClass('blue-highlight');
        $this.siblings().removeClass('blue-highlight');
      }
    });
    .hws-css-filter-td {
      border: solid 1px #000;
      height: 25px;
      width: 25px;
      float: left;
    }
    
    .hws-css-filter-tr {
      clear: left;
    }
    
    .blue-highlight {
      background-color: blue;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div class="hws-css-filter" >
      <div class="hws-css-filter-tr">
        <div class="hws-css-filter-td"></div>
        <div class="hws-css-filter-td"></div>
        <div class="hws-css-filter-td"></div>
      </div>
      <div class="hws-css-filter-tr">
        <div class="hws-css-filter-td"></div>
        <div class="hws-css-filter-td"></div>
        <div class="hws-css-filter-td"></div>
      </div>
    </div>

答案 3 :(得分:0)

基于Figor的答案,但也切换突出显示的单元格 https://jsfiddle.net/dwsL9z63/5/

   $('.hws-css-filter-td').click(function () {
      if($(this).hasClass("cell_highlight")){
        $(this).removeClass("cell_highlight");
      }
      else{
         $(this).addClass("cell_highlight");
      }

      $(this).siblings().removeClass("cell_highlight");

   });