This article shares the specific code for JavaScript to achieve dynamic color change of the table for your reference. The specific content is as follows The table is divided into two parts: the header and the body of the table. When you move to a row in the table body, the row you move to changes color to distinguish the number of rows you have selected, making it easier to see which row is selected. Implementation ideas 1. Get all rows of the table body Note: No exclusive thinking is needed here. If exclusive thinking is used, the last line that the mouse leaves will have color. Code example: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Dynamic color change of table</title> <style> th { background-color: pink; } .bg { background-color: green; } </style> </head> <body> <table border="1" cellpadding="8" cellspacing="0" align="center"> <thead> <tr> <th>Fruit</th> <th>Like level</th> <th>Season</th> </tr> </thead> <tbody> <tr> <td>Apple</td> <td>☆☆☆☆☆</td> <td>Four Seasons</td> </tr> <tr> <td>Banana</td> <td>☆☆☆</td> <td>Four Seasons</td> </tr> <tr> <td>Grapes</td> <td>☆☆☆</td> <td>Summer</td> </tr> <tr> <td>Durian</td> <td>☆☆☆☆☆☆☆☆</td> <td>Summer</td> </tr> <tr> <td>Mango</td> <td>☆☆☆☆☆☆☆☆</td> <td>Summer</td> </tr> <tr> <td>Watermelon</td> <td>☆☆☆☆☆</td> <td>Summer</td> </tr> </tbody> </table> <script> var tr = document.querySelector('tbody').querySelectorAll('tr'); for (var i = 0; i < tr.length; i++) { tr[i].onmouseover = function() { /* Exclusive algorithm for (var i = 0; i < tr.length; i++) { tr[i].className = ''; } */ this.className = 'bg'; } tr[i].onmouseout = function() { this.className = ''; } } </script> </body> </html> Page effect: The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM. You may also be interested in:
|
<<: Detailed example of MySQL joint table update data
>>: How to view nginx configuration file path and resource file path
Preface MySQL database lock is an important means...
Preface I have been summarizing my front-end know...
Table of contents Preface 1. Basic knowledge of d...
Table of contents What is the rest operator? How ...
Achieve results Implementation Code html <div ...
In fact, there are many corresponding writing met...
When the same function and HTML code are used mul...
Today, let's talk about how to use js to achi...
Table of contents 1. What is scrapy_splash? 2. Th...
Today, when learning PHP, of course, you have to ...
This article example shares the specific code for...
What is keepalive? In normal development, some co...
Key Takeaways: 1. Mastering CSS3 3D animation 2. ...
MySQL5.6.40 installation process under CentOS7 64...
This article shares the specific code of Vue to i...