javascript - 悬停时更改 CSS

标签 javascript html css

我尽量把它们分开。

<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/

相关文章:

html - 高度完全填充的 Div 列

javascript - 通过 spotify web api 和 javascript 播放歌曲时出现问题

javascript - 使用each-in循环后无法操作输入ember中的对象模型

javascript - :hover property not working on Firefox/IE

c# webbrowser 单击表单中的单选按钮

jquery - 如何使用 jQuery 隐藏不包含数据类特定值的所有元素?

html - 如何防止 HTML 中的 Division 标签扭曲

javascript - 使用 HTML、CSS、JS 形式自动更新进度条

html - 用 CSS 画圈

HTML:我可以多行显示按钮文本吗?