我尽量把它们分开。
<div id="a" onmouseover="chbg('red')" onmouseout="chbg('white')">This will change b element</div>
<div id="b">This is element b</div>
<div id="f" onmouseover="chbg('blue')" onmouseout="chbg('white')">This will change g element</div>
<div id="g">This is element g</div>
<div id="j" onmouseover="chbg('yellow')" onmouseout="chbg('white')">This will change k element</div>
<div id="k">This is element k</div>
这是JS
function chbg(color) {
document.getElementById('b').style.backgroundColor = color;
document.getElementById('g').style.backgroundColor = color;
document.getElementById('k').style.backgroundColor = color;
}
这里不能正常工作 http://jsfiddle.net/qUzn5/ .
最佳答案
您可能想像这样在函数中添加第二个参数:
JavaScript:
function chbg(color, id) {
document.getElementById(id).style.backgroundColor = color;<br/>
}
HTML:
(重复 div)
<div id="a" onmouseover="chbg('red', 'a')" onmouseout="chbg('white')">This will change b element</div>
关于javascript - 悬停时更改 CSS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24922206/