2014-03-01 91 views

回答

2

如果該元素具有相同的父,你可以使用「+」或「〜」 CSS選擇器

img:hover + button{color:red} 

http://jsfiddle.net/K4U9p/

+0

假設'button'跟在'img'標籤。 – akinuri

1

如果這些元素不具有相同的父,你可以使用, jQuery中:

$img.hover(function() { //mouse enter 
    $button.addClass('hover'); 
}, function() { //mouse leave 
    $button.removeClass('hover'); 
}); 

要刪除的事件處理程序:

$img.off('mouseenter mouseleave'); 
1

如果你想使用(純)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 = ""; 
    } 
} 

小提琴:Two func.Single Func.

相關問題