当一行中只有一个单元格被选中时,是否可以使用css和样式来改变html表格中整行的背景?

iszxjhcz  于 2023-01-18  发布在  其他
关注(0)|答案(2)|浏览(166)

我希望能够区分用户在html表格中选择的单元格的行。
有没有办法用CSS和样式来实现这一点,或者我需要用JQuery和JavaScript来编程?
例如,用户可以点击第一行的BA单元格,然后我希望第一行的背景是浅蓝色。
example screenshot

var $TABLE = $('#table');
var $BTN = $('#export-btn');
var $EXPORT = $('#export');
$('.table-add').click(function() {
  var $clone = $TABLE.find('tr.hide').clone(true).removeClass('hide table-line');
  $TABLE.find('table').append($clone);
});
$('.table-remove').click(function() {
  $(this).parents('tr').detach();
});
$('.table-up').click(function() {
  var $row = $(this).parents('tr');
  if ($row.index() === 1) return;
  $row.prev().before($row.get(0));
});
$('.table-down').click(function() {
  var $row = $(this).parents('tr');
  $row.next().after($row.get(0));
});
jQuery.fn.pop = [].pop;
jQuery.fn.shift = [].shift;
$BTN.click(function() {
  var $rows = $TABLE.find('tr:not(:hidden)');
  var headers = [];
  var data = [];
  $($rows.shift()).find('th:not(:empty)').each(function() {
    headers.push($(this).text().toLowerCase());
  });
  $rows.each(function() {
    var $td = $(this).find('td');
    var h = {};
    headers.forEach(function(header, i) {
      h[header] = $td.eq(i).text();
    });
    data.push(h);
  });
});
@import url('https://fonts.googleapis.com/css?family=Montserrat:400,500');
body {
  font-family: 'Montserrat', sans-serif;
  padding: 0;
  margin: 0;
  text-align: center;
}

h1 {
  position: relative;
  padding: 0;
  margin: 10;
  font-family: "Raleway", sans-serif;
  font-weight: 300;
  font-size: 40px;
  color: #080808;
  -webkit-transition: all 0.4s ease 0s;
  -o-transition: all 0.4s ease 0s;
  transition: all 0.4s ease 0s;
}

tr:nth-of-type(odd) {
  background: #eee;
}

th {
  background: #3498db;
  color: white;
  font-weight: bold;
}

@import "compass/css3";
.table-editable {
  position: relative;
  .glyphicon {
    font-size: 20px;
  }
}

table {
  width: 750px;
  border-collapse: collapse;
  margin: 50px auto;
}

td,
th {
  padding: 10px;
  border: 1px solid #ccc;
  text-align: left;
  font-size: 18px;
}

.table-remove {
  color: #700;
  cursor: pointer;
  &:hover {
    color: #f00;
  }
}

.table-up {
  color: #007;
  cursor: pointer;
  &:hover {
    color: #00f;
  }
}

.table-down {
  color: #007;
  cursor: pointer;
  &:hover {
    color: #00f;
  }
}

.table-add {
  color: #070;
  cursor: pointer;
  position: absolute;
  top: 8px;
  right: 0;
  &:hover {
    color: #0b0;
  }
}
<! DOCTYPE html>
<html>

<head>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <meta charset="UTF-8">
  <title> JavaScript editable table </title>
  <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.css">
  <link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
</head>
<style>

</style>

<body>
  <div class="container">
    <h1> JavaScript Editable Table </h1>
    <div id="table" class="table-editable">
      <table class="table">
        <tr>
          <th> Name</th>
          <th> Roll No </th>
          <th> Class </th>
          <th> Marks </th>
        </tr>
        <tr>
          <td contenteditable="true"> Ram </td>
          <td contenteditable="true"> 1 </td>
          <td contenteditable="true"> BA </td>
          <td contenteditable="true"> 48 </td>
        </tr>
        <tr>
          <td contenteditable="true"> Rama </td>
          <td contenteditable="true"> 10 </td>
          <td contenteditable="true"> BSC </td>
          <td contenteditable="true"> 40 </td>
        </tr>
        <tr>
          <td contenteditable="true"> sham </td>
          <td contenteditable="true"> 8 </td>
          <td contenteditable="true"> BCA </td>
          <td contenteditable="true"> 34 </td>
        </tr>
        <tr>
          <td contenteditable="true"> shama </td>
          <td contenteditable="true"> 3 </td>
          <td contenteditable="true"> BCA </td>
          <td contenteditable="true"> 30 </td>
        </tr>
      </table>
    </div>
  </div>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js">
  </script>
</body>

</html>
js4nwp54

js4nwp541#

通过在样式中添加以下代码,使用has,选中的行将以不同的颜色显示,而不需要jquery或javascript代码。

tr:has(td:focus-visible) {
 background-color: aquamarine;
}
pexxcrt2

pexxcrt22#

jquery实现了一个has选择器,您可以选择所有具有特定子属性父对象

$('td[contenteditable="true"]').click(function() {
  $( "tr" ).has('td[contenteditable="true"]').removeClass('selected');
  $( "tr" ).has('td[contenteditable="true"]:focus').addClass('selected');
});

用于选择具有焦点的TD的选择器是td[contenteditable="true"]:focus
x一个一个一个一个x一个一个二个一个x一个一个三个一个

相关问题