如何重新启动?

时间:2012-08-07 12:25:01

标签: javascript jquery css

我有这个功能,一切都很好,但我再也无法启动它了。能做到这一次只有一次,有什么不对?求助。

$('.bottom_panel_button_05').one('click', function(){
    $(this).css(
        'background-image','url("img/bottom_panel_icons/ap_bottom_panel_back_button_01_135_on.png")',
        'background-position','center center no-repeat'
    ).addClass('panel_blocked');
    $(this).one('click', function(){
        $(this).css(
            'background-image','url("img/bottom_panel_icons/ap_bottom_panel_back_button_01_135.png")',
            'background-position','center center no-repeat'
        ).removeClass('panel_blocked');
    });
});

5 个答案:

答案 0 :(得分:2)

您在较旧的jQuery版本中使用的是函数.one()而不是.on()(或.delegate().live() / .bind()

答案 1 :(得分:1)

    $('.bottom_panel_button_05').on('click', function(){
      if (!$(this).hasClass('panel_blocked')) {
        $(this).css({
          'background-image': 'url("img/bottom_panel_icons/ap_bottom_panel_back_button_01_135_on.png")',
          'background-position': 'center center no-repeat'
        }).addClass('panel_blocked');
      } else {
        $(this).css({
          'background-image': 'url("img/bottom_panel_icons/ap_bottom_panel_back_button_01_135.png")',
          'background-position': 'center center no-repeat'
        }).removeClass('panel_blocked');
      }
    });

答案 2 :(得分:1)

看起来您正在尝试切换按钮,在这种情况下,您可以简化代码:

$('.bottom_panel_button_05').on('click', function (){
    var that = $(this);
    if (that.hasClass('panel_blocked') {
        that.css(
            'background-image','url("img/bottom_panel_icons/ap_bottom_panel_back_button_01_135.png")'
        ).removeClass('panel_blocked');
    } else {
        that.css(
            'background-image','url("img/bottom_panel_icons/ap_bottom_panel_back_button_01_135_on.png")'
        ).addClass('panel_blocked');
    }
});

答案 3 :(得分:1)

你所做的只是将'_on'切换到图像名称的末尾,并根据元素是否具有该类来切换类,一个简单的方法是:

$('.bottom_panel_button_05').on('click', function (){
    var Is = $(this).is('.panel_blocked');
    $(this).css('background-image','url("img/bottom_panel_icons/ap_bottom_panel_back_button_01_135'+(Is?'':'_on')+'.png")')
           .toggleClass('panel_blocked', !Is);
});​

答案 4 :(得分:0)

one更改为live

$('.bottom_panel_button_05').live('click', function(){
    $(this).css(
        'background-image','url("img/bottom_panel_icons/ap_bottom_panel_back_button_01_135_on.png")',
        'background-position','center center no-repeat'
    ).addClass('panel_blocked');
    $(this).live('click', function(){
        $(this).css(
            'background-image','url("img/bottom_panel_icons/ap_bottom_panel_back_button_01_135.png")',
            'background-position','center center no-repeat'
        ).removeClass('panel_blocked');
    });
});