繁体   English   中英

使用onclick事件更改onmouseover命令

[英]Change onmouseover command with onclick event

我的页面中有以下行:

 <div id="fmeFriendStatus" style="width:589px;height:700px;overflow:auto" onmouseover="DelayedStatusRefresh()">

我希望能够改变:

onmouseover="DelayedStatusRefesh()"

命令:

onmouseover="DelayedStatusRefeshFriends()"

点击页面上其他地方的按钮...

这有可能,你会怎么做?

提前谢谢了

快速回答如下:

var button = document.getElementById('someButtonId');

button.addEventListener('click', function() {
  document.getElementById('fmeFriendStatus').onmouseover = DelayedStatusRefeshFriends;
});

但是,出于多种原因,直接访问onclick或其他on*属性来监听事件是不受欢迎的。 理想情况下,您可以使用addEventListener() (或者使用JavaScript库,如jQuery)将DelayedStatusRefesh添加为事件侦听器,并在单击该按钮时删除该侦听removeEventListener() )并添加另一个。

您需要将onclick处理程序添加到第二个按钮,其处理程序需要为fmeFriendStatus元素重新分配mouseover处理程序。

所以它应该是这样的:

document.getElementById("buttonFromSomewhereElse").onclick = function() {
    document.getElementById("fmeFriendStatus").onmouseover = DelayedStatusRefeshFriends;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM