当我将鼠标悬停在另一行/段上时,如何更改表的一行/段?

时间:2016-10-24 21:48:56

标签: html css html-table

site上有两张桌子。

我想这样做,当我将鼠标悬停在第一个表中的生产者ID上时,它会更改第二个表中生产者ID行的颜色。

1 个答案:

答案 0 :(得分:0)

I think you will have to use some simple javascript events.

Pure JS:

var one = document.getElementsByClassName("one")[0];
var two = document.getElementsByClassName("two")[0];

one.addEventListener("mouseenter", function() {
    two.classList.toggle("red");
});

one.addEventListener("mouseleave", function() {
    two.classList.toggle("red");
});

https://jsfiddle.net/zv9r7oL2/1/

This is made even easier with jQuery:

$(".one").hover(function() {
    $(".two").toggleClass('red');
});

https://jsfiddle.net/zv9r7oL2/

Same with tables: https://jsfiddle.net/zv9r7oL2/2/

Edit: Also, such events should be handled after the document is fully loaded. So for pure JS:

window.onload = function() {
 //put all your code here
}

for jQuery:

$(document).ready(function() {
//your code here
}

And dont forget to link the jQuery library to use it :) add this to your < head > html

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>