在jquery中取消绑定mouseout事件?

时间:2012-02-18 16:06:03

标签: jquery html css

你好!

我收到了以下代码:

function UpdatePriceSubscribeButton() {

    if (_userPriceSubscribe > 0) {
        $("#btUpdatePriceSubscribing").removeClass("con");
        $("#btUpdatePriceSubscribing").addClass("conActive");
        $("#btUpdatePriceSubscribing").unbind('onmouseover').unbind('onmouseout');
    }
    else {
        $("#btUpdatePriceSubscribing").removeClass("conActive");
        $("#btUpdatePriceSubscribing").addClass("con");
        $("#btUpdatePriceSubscribing").mouseover(function() { this.className = 'conActive'});
        $("#btUpdatePriceSubscribing").mouseout(function() {this.className = 'con'});
    }
};

这个问题是,当onmouseout时,即使_userPriceSubscribe设置为1,该类也会被更改?

我在这里尝试做的是使用jquery在客户端更改当前类和悬停类。

编辑1:我还改编了$(“#btUpdatePriceSubscribing”)。unbind('omouseenter mouseleave');在这里消化:http://api.jquery.com/hover/

编辑2:这有效:

function UpdatePriceSubscribeButton() {

    if (_userPriceSubscribe > 0) {
        $("#btUpdatePriceSubscribing").removeClass("con");
        $("#btUpdatePriceSubscribing").addClass("conActive");
        $("#btUpdatePriceSubscribing").unbind('omouseenter mouseleave');
    }
    else {
        $("#btUpdatePriceSubscribing").removeClass("conActive");
        $("#btUpdatePriceSubscribing").addClass("con");

        $("#btUpdatePriceSubscribing").hover(function () { this.className = 'conActive' }, function () { this.className = 'con' });
    }
};

但这是一个很好的方法吗?

BestRegards

3 个答案:

答案 0 :(得分:1)

设置一个 onmouseout / onmouseover处理程序在mouseout处理程序中查找价格是否有意义?

$("#btUpdatePriceSubscribing").mouseover(function () {
   if (_userPriceSubscribe > 0) {
       this.className = "..."
   }
}

(否则我不确定我会得到你想做的......)

答案 1 :(得分:1)

事件名称为mouseenter而不是omouseenter。因此,除非omouseenter是自定义事件,否则您需要更改此行:

$("#btUpdatePriceSubscribing").unbind('omouseenter mouseleave');

为:

$("#btUpdatePriceSubscribing").unbind('mouseenter mouseleave');

答案 2 :(得分:0)

这解决了我的问题:

function UpdatePriceSubscribeButton() {

    if (_userPriceSubscribe > 0) {
        $("#btUpdatePriceSubscribing").removeClass("con");
        $("#btUpdatePriceSubscribing").addClass("conActive");
        $("#btUpdatePriceSubscribing").unbind('omouseenter mouseleave');
    }
    else {
        $("#btUpdatePriceSubscribing").removeClass("conActive");
        $("#btUpdatePriceSubscribing").addClass("con");

        $("#btUpdatePriceSubscribing").hover(function () { this.className = 'conActive' }, function () { this.className = 'con' });
    }
};
相关问题