如何将悬停事件同时应用于多个元素?

时间:2019-12-11 07:04:01

标签: javascript jquery html

我对JavaScript非常陌生,希望将表数据按行进行分组。每行有6个表数据。我希望前三个链接在一起,而后三个分开。为我想要链接在一起的列创建className会使所有表都悬停。我只希望特定的行值是可悬停的。

var table = document.getElementsByTagName("tbody");

for (var i = 0; i < table.length-1; i++){

    var currentTable = table[i];
    var rows = currentTable.getElementsByTagName("tr");

    for(var j = 3; j < rows.length-1; j++){

        var c = rows[j].cells;

        c[0].className = "fall";
        c[1].className = "fall";
        c[2].className = "fall";
        // for each row make these three selectable

        c[3].className = "spring";
        c[4].className = "spring";
        c[5].className = "spring";
    }
}

除了为每行创建一个新的类名之外,我不确定是否有更好的方法来做到这一点。如果我需要这样做,是否有一种简单的方法可以动态地做到这一点?如果您想查看实际的表格,请告诉我。谢谢。

1 个答案:

答案 0 :(得分:2)

当然,您可以使用CSS通过基于索引对它们进行分类:

td:nth-child(1), td:nth-child(2), td:nth-child(3) {
  color: red;
}

td:nth-child(4), td:nth-child(5), td:nth-child(6) {
  color: green;
}
<table>
  <tbody>
    <tr>
      <td>Cell 1</td>
      <td>Cell 2</td>
      <td>Cell 3</td>
      <td>Cell 4</td>
      <td>Cell 5</td>
      <td>Cell 6</td>
    </tr>
    <tr>
      <td>Cell 7</td>
      <td>Cell 8</td>
      <td>Cell 9</td>
      <td>Cell 10</td>
      <td>Cell 11</td>
      <td>Cell 12</td>
    </tr>
  </tbody>
</table>

参考::nth-child()


更新:

$('td:nth-child(1),td:nth-child(2),td:nth-child(3)').on('mouseover', function () {
  var index = $(this).index() + 1;
  
  $('table td:nth-child(' + index + ')').css('color', 'red');
});

$('td:nth-child(1),td:nth-child(2),td:nth-child(3)').on('mouseout', function () {
  var index = $(this).index() + 1;
  
    $('table td:nth-child(' + index + ')').css('color', 'black');
});

$('td:nth-child(4),td:nth-child(5),td:nth-child(6)').on('mouseover', function () {
  var index = $(this).index() + 1;
  
  $('table td:nth-child(' + index + ')').css('color', 'green');
});

$('td:nth-child(4),td:nth-child(5),td:nth-child(6)').on('mouseout', function () {
  var index = $(this).index() + 1;
  
    $('table td:nth-child(' + index + ')').css('color', 'black');
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<h3>Table 1</h3>

<table>
  <tbody>
    <tr>
      <td>Cell 1</td>
      <td>Cell 2</td>
      <td>Cell 3</td>
      <td>Cell 4</td>
      <td>Cell 5</td>
      <td>Cell 6</td>
    </tr>
  </tbody>
</table>

<h3>Table 2</h3>

<table>
  <tbody>
    <tr>
      <td>Cell 7</td>
      <td>Cell 8</td>
      <td>Cell 9</td>
      <td>Cell 10</td>
      <td>Cell 11</td>
      <td>Cell 12</td>
    </tr>
  </tbody>
</table>


更新2:

$('td:nth-child(1),td:nth-child(2),td:nth-child(3)').on('mouseover', function () {

  $('td:nth-child(1),td:nth-child(2),td:nth-child(3)').css('color', 'red');
  
});

$('td:nth-child(1),td:nth-child(2),td:nth-child(3)').on('mouseout', function () {

  $('td:nth-child(1),td:nth-child(2),td:nth-child(3)').css('color', 'black');
  
});

$('td:nth-child(4),td:nth-child(5),td:nth-child(6)').on('mouseover', function () {

    $('td:nth-child(4),td:nth-child(5),td:nth-child(6)').css('color', 'green');
    
});

$('td:nth-child(4),td:nth-child(5),td:nth-child(6)').on('mouseout', function () {

    $('td:nth-child(4),td:nth-child(5),td:nth-child(6)').css('color', 'black');


});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<h3>Table 1</h3>

<table>
  <tbody>
    <tr>
      <td>Cell 1</td>
      <td>Cell 2</td>
      <td>Cell 3</td>
      <td>Cell 4</td>
      <td>Cell 5</td>
      <td>Cell 6</td>
    </tr>
  </tbody>
</table>

<h3>Table 2</h3>

<table>
  <tbody>
    <tr>
      <td>Cell 7</td>
      <td>Cell 8</td>
      <td>Cell 9</td>
      <td>Cell 10</td>
      <td>Cell 11</td>
      <td>Cell 12</td>
    </tr>
  </tbody>
</table>

或者,如果您只想在1行上使用该事件,则可以将该事件编辑为:

$('td:nth-child(1),td:nth-child(2),td:nth-child(3)').on('mouseover', function () {

  $(this).parent().find('td:nth-child(1),td:nth-child(2),td:nth-child(3)').css('color', 'red');

});

更新3:

$('td').on('mouseover', function () {
  var tr = $(this).parent();
  
  var index = $(this).index();
  
  var cells = index < 3 ? [1, 2, 3] : [4, 5, 6];
  
  var color = index < 3 ? 'red' : 'green';
  
  cells.forEach(function (x) {
    tr.find('td:nth-child(' + x + ')').css('color', color);
  });
});

$('td').on('mouseout', function () {
  $(this).parent().find('td').css('color', 'black');
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<h3>Table 1</h3>

<table>
  <thead>
    <tr>
      <th>H1</th>
      <th>H2</th>
      <th>H3</th>
      <th>H4</th>
      <th>H5</th>
      <th>H6</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Cell 1</td>
      <td>Cell 2</td>
      <td>Cell 3</td>
      <td>Cell 4</td>
      <td>Cell 5</td>
      <td>Cell 6</td>
    </tr>
  </tbody>
</table>

<h3>Table 2</h3>

<table>
  <thead>
    <tr>
      <th>H1</th>
      <th>H2</th>
      <th>H3</th>
      <th>H4</th>
      <th>H5</th>
      <th>H6</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Cell 7</td>
      <td>Cell 8</td>
      <td>Cell 9</td>
      <td>Cell 10</td>
      <td>Cell 11</td>
      <td>Cell 12</td>
    </tr>
  </tbody>
</table>