简体   繁体   English

jQuery帮助处理鼠标事件,事件不会解除绑定

[英]Jquery help with mouse events, events not unbinding

Any ideas why this doesn't work, or how to make it work? 有什么想法为什么不起作用,或者如何使其起作用? I want to remove the "onmouseover" and "onmouseout" events so they are basically disabled and at the same time change the background color. 我想删除“ onmouseover”和“ onmouseout”事件,以便基本上禁用它们,同时更改背景色。 Then when the user clicks on another element I want to reassign the mouse events back to the element. 然后,当用户单击另一个元素时,我想将鼠标事件重新分配给该元素。 Right now the onmouse events don't get disabled at all, the background doesn't change, etc. 现在,onmouse事件根本不会被禁用,背景也不会改变,等等。

Here's how I call the function: 这是我调用该函数的方式:

Here's the function: $(document).ready(function(){ 这是函数:$(document).ready(function(){

$(".maximize").toggle(

   function(){ 

    $("#property_bg").unbind("onmouseover");
    $("#property_bg").unbind("onmouseout"); 
    $("#property_bg").toggleClass("body_bgcolor");
   }, 

       function() { 

        $("#property_bg").bind("onmouseover", function() {        
                              swap_class("property_bg","body_bgcolor")} );      

   });

}); });

Thanks for the help. 谢谢您的帮助。

Remove the "on" from the event names. 从事件名称中删除“ on”。 Then it'll work. 这样就可以了。

在您的活动上,将鼠标移开或移出鼠标即可。

I found out the real problem in the following two threads for anyone who comes along with a similar problem I will add them here: 对于存在类似问题的任何人,我在以下两个线程中发现了真正的问题,我将在此处添加它们:

How do I unbind "hover" in jQuery? 如何在jQuery中取消绑定“悬停”?

Why this unbind doesn't work? 为什么取消绑定不起作用?

The problem was solved by not hard coding the mouse events in the HTML, but rather binding them in the document.ready 1st. 通过不对HTML中的鼠标事件进行硬编码,而是将它们绑定到document.ready 1st中,解决了该问题。 In order to "unbind" and event, the event has to be "binded" by jquery. 为了与事件“解除绑定”,事件必须由jquery进行“捆绑”。

Also, "mouseover" didn't work for some reason I couldn't figure out, but when I put "mouseenter" and "mouseleave" as suggested in one of the posts above it worked. 另外,由于某些原因我无法弄清楚“ mouseover”没有用,但是当我按照上面其中一篇文章中的建议放置“ mouseenter”和“ mouseleave”时,它起作用了。 I've never heard of "mouseenter" or "mouseleave", but ... it works now. 我从未听说过“ mouseenter”或“ mouseleave”,但是...现在可以使用了。

Good luck! 祝好运!

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

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