Javascript 如何定义onMouseOver和onMouseOut内部的函数 登记

Javascript 如何定义onMouseOver和onMouseOut内部的函数 登记,javascript,html,css,Javascript,Html,Css,当鼠标悬停事件触发时,会出现以下错误: 未捕获的语法错误:意外 代币( 我应该怎么做?我想在onMouseOver和onMouseOut中定义一个函数不要使用函数声明,只需执行以下操作: <div align="right" style="border:1 #FF0 solid; background-color:#999" onMouseOver="javascript: function(){this.style.backgroundColor = '#DDD';}" onMouseO

当鼠标悬停事件触发时,会出现以下错误:

未捕获的语法错误:意外 代币(


我应该怎么做?我想在
onMouseOver
onMouseOut
中定义一个函数不要使用
函数
声明,只需执行以下操作:

<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>
以及:


顺便说一下,你应该考虑<强> < /强> ./p> 为什么在你的场景中想要一个函数?你可以使用.< /p>
onMouseOut=“javascript:this.style.backgroundColor='#999'>

您可以定义外部javascript方法,该方法接受所需的参数,并对多个元素使用此方法:

onMouseOut = "this.style.backgroundColor = '#999';"

函数OnMouseDownEventHandler(发送方){
sender.style.backgroundColor='#DDD';
}

您应该看看jQuery并使用不引人注目的javascript

<script language="javascript" type="text/javascript">
function OnMouseDownEventHandler(sender) {
    sender.style.backgroundColor = '#DDD';
}
</script>

<div onmousedown="OnMouseDownEventHandler(this);"></div>
然后你可以有一个外部脚本,比如app.js

#container {border:1 #FF0 solid; background-color:#999;text-align:left;}
不要忘记将外部css和js链接到页面。外部样式表需要在jQuery脚本之后链接到

$('#container').mouseout(function() {
  $('#container').css('backgroundColor', '#DDD')
});

这里有很多主题:

为什么不直接使用css呢

元素:hover(){ }

或者是您需要的js函数,而bg只是一个示例?…在这种情况下,您可以使用jQuery获得一个简单的结果 $(“#元素”).mouseover(函数(){ $(this.addClass(“bghover”); }))

$(“#元素”).mouseoutfunction(){ $(this.removeClass(“bghover”);
});

我知道,但我只是想尝试一些不同的方法:)这看起来比调用函数更有效;)不,我不想调用函数
<div id="container"> Register </div>
#container {border:1 #FF0 solid; background-color:#999;text-align:left;}
$('#container').mouseout(function() {
  $('#container').css('backgroundColor', '#DDD')
});
http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js
<script src="app.js" type="text/javascript"></script>
<link rel="stylesheet" type="text/css" href="app.css">
$('#container').mouseout($('#container').css('backgroundColor', '#DDD'))
$('#container').mouseover($('#container').css('backgroundColor', '#DDD'))
$('#container').mouseenter($('#container').css('backgroundColor', '#DDD'))
$('#container').mouseup($('#container').css('backgroundColor', '#DDD'))