<!doctype html>
<html>
<head>
<title></title>
<!--binding handlers to object-->
<script type="text/javascript">
<!--
function showAuthor()
{
alert("oscar wilde");
}
function enableEvent()
{
document.getElementById('someText').attachEvent("onmouseover",showAuthor);
}
function disableEvent()
{
someText.detachEvent("onmouseover",showAuthor);
}
//-->
</script>
</head>
<body onload="enableEvent();">
<em id="someText">we may be in the gutter ,but some of us are looking at the stars</em>
<form action="#" method="get">
<input type="button" value="Attach event" onclick="enableEvent();" />
<input type="button" value="Detach event" onclick="disableEvent();" />
<input type="button" value="show author" onclick="showAuthor();" />
</form>
</body>
</html>
我已经使用上述代码来实现事件处理程序,但是当我执行程序事件处理程序没有附加。为什么?事件没有被添加
你似乎在'onclick'上附加了'mouseover'事件。所以,你是否试图仅在'mouseover'上显示'showAuthor',并且只在第一次点击元素时显示'showAuthor'? –