如何在onMouseOver和onMouseOut中定义函数

Yun*_*zel 0 html javascript css

<div align="right" style="border:1 #FF0 solid; background-color:#999" onMouseOver="javascript: function(){this.style.backgroundColor = '#DDD';}" onMouseOut="javascript: function(){this.style.backgroundColor = '#999';}">
Register
</div>
Run Code Online (Sandbox Code Playgroud)

当mouseover事件触发时,会出现此错误:

未捕获的SyntaxError:意外的令牌(

我该怎么办?我想里面定义一个函数onMouseOveronMouseOut

Sar*_*raz 5

不要使用function声明,只需:

onMouseOver = "this.style.backgroundColor = '#DDD';"
Run Code Online (Sandbox Code Playgroud)

和:

onMouseOut = "this.style.backgroundColor = '#999';"
Run Code Online (Sandbox Code Playgroud)

顺便说一句,你应该考虑不引人注目的JavaScript.