我有一个问题,我正在为 onmouseout 事件分配一个函数,但是在运行该事件之后,我需要将其删除。非常感谢您的帮助。
3 回答
这取决于您的代码,如果您使用 d3 执行此操作,那么您可以说
在您的 onmouseout 事件函数中:
element.on("mouseout",func);
function func(){
/*do your stuff*/
element.on("mouseout",null);
}
如果您通过事件属性绑定事件,<div onmouseout="..." >
那么您必须重构它。在这种情况下,只需使用 d3on()
函数来绑定事件。
如果您想将多个处理程序绑定到同一个事件,您可以使用命名空间:通过附加.name
到事件,您可以更具体地处理它们。
我假设你用d3
's on
event添加了你的事件监听器。他们删除事件的文档是这样的:
如果已在选定元素上为相同类型注册了事件侦听器,则在添加新侦听器之前删除现有侦听器。要为同一事件类型注册多个侦听器,该类型后面可以跟一个可选的命名空间,例如“click.foo”和“click.bar”。要删除侦听器,请将 null 作为侦听器传递。
我无法更正您的代码,因为您没有添加任何代码。
Another way is to use jQuery:
$(element).bind("mouseout", myFunction);
$(element).unbind("mouseout");
You can also use an optional namespace in the first parameter like:
"mouseout.myNamespace"
With this namespace you can bind/unbind multiple events of the same kind.
Another thing that maybe is interesting to you: jQuery stopPropagation