【发布时间】:2013-01-30 19:14:36
【问题描述】:
当我单击一个元素时,我想取消绑定“mouseenter”和“mouseleave”事件,这可以正常工作,但如果单击另一个元素,我想将它们重新绑定 - 这不起作用。
有什么帮助吗?
代码如下:
<script type="text/javascript">
$(document).ready(function(){
$("#shape1 img").click(function(){
$("#shape1 img,#shape2 img, #shape3 img, #shape4 img, #shape5 img").unbind('mouseenter mouseleave');
});
$("#close").click(function(){
$("#shape1 img,#shape2 img, #shape3 img, #shape4 img, #shape5 img").bind('mouseenter mouseleave');
});
});
</script>
非常感谢!
【问题讨论】:
-
您需要在绑定中关联一个函数,例如。 bind('mouseenter mouseleave', function() { $(this).toggleClass('entered'); });
-
(a) 你为什么使用
bind/unbind而不是on/off? (b) 要附加一个事件处理程序,您需要指定一个!