![](/img/trans.png)
[英]Make an element change colour over time that is only visible when the element is hovered, but independent of the hover itself
[英]How do I change :hover status on an element when a different element is hovered over?
<img>
<button>
button:hover
color: blue
我希望将img悬停在按钮的:hover效果上。 可以在CSS中完成吗? 如果没有,那么JS中最简单的方法是什么(jQuery很好)?
如果元素没有相同的父代,则可以在jQuery中使用:
$img.hover(function() { //mouse enter
$button.addClass('hover');
}, function() { //mouse leave
$button.removeClass('hover');
});
删除事件处理程序:
$img.off('mouseenter mouseleave');
如果您想使用(纯)JS
var button = document.getElementById("button");
var img = document.getElementById("img");
img.onmouseover = modifyButton;
img.onmouseout = resetButton;
function modifyButton() {
button.style.color = "red";
}
function resetButton() {
button.style.color = "";
}
或者您可以使用一个功能
img.onmouseout = modifyButton;
function modifyButton() {
if (button.style.color != "red") {
button.style.color = "red"
} else {
button.style.color = "";
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.