2015-08-31 35 views
0

我有一个mouseover事件的问题(当然,如果我在DIV上使用它,每个事件) 我的问题是我想改变一个div的ClassName,通过使用鼠标用javascriptoverwrite改变div的classname

我的HTML是一样的东西:

<head> 
<script src="code"></script> 
</head>  
<body> 
    <div>Stuff</div> 
    <div id="box" class="meme">More stuff</div> 
</body> 

,我的JavaScript是这个

function change(){ 
document.getElementById("box").className = "otherName"; 

} 
window.onload= function(){ 

document.getElementById("box").onmouseover = change;  

}; 

谢谢您的时间:)

+0

You c ode似乎在工作:http://jsfiddle.net/auaLu7f8/ –

+0

尝试监听事件:document.getElementById(“box”)。addEventListener('mouseover',change); – WebArtisan

+0

观察到的预期行为是什么? – marekful

回答

0

您的代码正在工作。只需添加CSS类并验证。让我知道如果期望是别的。

function change(){ 
 
document.getElementById("box").className = "otherName"; 
 

 
} 
 
window.onload= function(){ 
 

 
document.getElementById("box").onmouseover = change;  
 

 
};
.otherName{color:red;}
<div>Stuff</div> 
 
<div id="box" class="meme">More stuff</div>

0

我相信你的标记是问题,而不是JS代码,你有下面这行:

<script src="code"></script> 

这是没有意义的,我相信你没有加载您的页面中的js代码,请尝试如下:

<html> 
<head> 
<script type="text/javascript"> 
    function change(){ 
     document.getElementById("box").className = "otherName"; 
    } 
    window.onload= function(){ 

     document.getElementById("box").onmouseover = change;  

    }; 
</script> 
<style> 
    .meme { 
     color: red 
    } 

    .otherName { 
     color: blue 
    } 
</style> 
</head>  
<body> 
    <div>Stuff</div> 
    <div id="box" class="meme">More stuff</div> 
</body> 
</html>