且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

鼠标悬停时更改背景图像

更新时间:2023-11-11 11:30:10

如果要求一个元素影响另一个元素的背景,则需要 JS.假设 id="news" 是触发元素,id="BG" 是接收元素:

If the requirement is that one element affects the background of another, you will require JS. Assuming id="news" is the triggering element and id="BG" is the receiving element:

document.getElementById("news").addEventListener("mouseover", function() {
  document.getElementById("BG").style.backgroundImage = "url(img/southamericangrandmother.jpg)";
}, false);
document.getElementById("news").addEventListener("mouseout", function() {
   document.getElementById("BG").style.backgroundImage = "";
}, false);