How to highlight rows in a table using jquery

I have a simple jquery script to highlight a DOM element on hover. But this script was not able to select the rows of my table, there are no problems with the cells.

In my script, I need to select any type of elements, not just tables, so I can not write a solution based on the choice of a table, for example DataTables . Any suggestions?

$(document).ready(function() {
  $("body").on('mouseover', function(event) {
    var highlightTarget = $(event.target);
    highlightTarget.addClass("highlight");

  }).on('mouseout', function(event) {
    $(event.target).removeClass('highlight');
  });
});
.highlight {
  border: 1px solid green;
  background-color: darkseagreen;
  z-index: 99999;
}
.main {
  border-top: 1px solid #9EBACF;
  border-bottom: 1px solid #FFFFFF;
  border-left: 1px solid #9EBACF;
  border-right: 1px solid #FFFFFF;
}
.cat {
  border-top: 1px solid #FFFFFF;
  border-bottom: 1px solid #9EBACF;
  border-left: 1px solid #FFFFFF;
  border-right: 1px solid #9EBACF;
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table class="main" cellspacing="0" cellpadding="4">
  <tr>
    <td class="cat">data 1</td>
    <td class="cat">data 2</td>
  </tr>
  <tr>
    <td class="cat">data 3</td>
    <td class="cat">data 4</td>
  </tr>
  <tr>
    <td class="cat">data 5</td>
    <td class="cat">data 6</td>
  </tr>
</table>
Run codeHide result
+4
source share
3 answers

One way to do this with CSS is to use a selector :hover.

.hoverable:hover {
    background: rgba(150, 150, 150, 0.5);
}

.hoverable . , <tr>, <td>. <td>, <tr>.

.hoverable:hover {
  background: rgba(180, 180, 180, 0.5);
}
<table class="main" cellspacing="0" cellpadding="4">
  <tr class="hoverable">
    <td class="hoverable">data 1</td>
    <td class="hoverable">data 2</td>
  </tr>
  <tr>
    <td class="hoverable">data 3</td>
    <td class="hoverable">data 4</td>
  </tr>
  <tr class="hoverable">
    <td>data 5</td>
    <td>data 6</td>
  </tr>
</table>
Hide result
+5

  • .
  • , .
  • , , , .
  • , .
  • , .
  • .

: 6, div. div table td, .

$(document).ready(function() {
  createHover()
});

function createHover() {
  const map = {
    "TD": "tr"
  }
  $(document).on('mouseenter mouseout', '*', function(e) {
    var myClass = "highlight"
    var parent = map[this.nodeName];
    var $this = $(this)
    var el = $this;
    $('.' + myClass).removeClass(myClass)
    if (parent) {
      el = $this.closest(parent)
    }
    el.toggleClass(myClass, $this.is(":hover"))
    e.stopPropagation()
  })
}
.highlight {
  border: 1px solid green;
  background-color: darkseagreen;
}
.main {
  border-top: 1px solid #9EBACF;
  border-bottom: 1px solid #FFFFFF;
  border-left: 1px solid #9EBACF;
  border-right: 1px solid #FFFFFF;
}
.cat {
  border-top: 1px solid #FFFFFF;
  border-bottom: 1px solid #9EBACF;
  border-left: 1px solid #FFFFFF;
  border-right: 1px solid #9EBACF;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>

<body>
  <table class="main" cellspacing="0" cellpadding="4">
    <tr>
      <td class="cat">
        <strong>Edge case</strong>
      </td>
      <td class="cat">data 2</td>
    </tr>
    <tr>
      <td class="cat">data 3</td>
      <td class="cat">data 4</td>
    </tr>
    <tr>
      <td class="cat">data 5</td>
      <td class="cat">data 6</td>
    </tr>
  </table>
  <ul>
    <li>This is a test</li>
  </ul>
  <p>This is also a test</p>
</body>
Hide result
0

you don't need JS for this, a simple css hover will do this:

.cat:hover{
  border: 1px solid green;
  background-color: darkseagreen;
  z-index: 99999;
}

you don't need .highlighteither

0
source

Source: https://habr.com/ru/post/1667541/


All Articles