【问题标题】:jQuery - Activating one toggle button disables the otherjQuery - 激活一个切换按钮会禁用另一个
【发布时间】:2013-09-12 21:22:00
【问题描述】:

我遇到了两个 jQuery 切换按钮的问题。它们用于移动导航按钮:

(function($) {
    $(function() {
$('#mobile-nav-button').toggle(
    function() {
        $('#fullpage').animate({ left: 250 }, 'normal', function() {
            $('#mobile-nav-button').html('Close');{
            $('#social-nav-panel').hide();
        }
        });
    },
    function() {
        $('#fullpage').animate({ left: 0 }, 'normal', function() {
            $('#mobile-nav-button').html('Open');
        });
    }
);
$('#social-nav-button').toggle(
    function() {
        $('#fullpage').animate({ right: 250 }, 'normal', function() {
            $('#social-nav-button').html('Close');
        });
    },
    function() {
        $('#fullpage').animate({ right: 0 }, 'normal', function() {
            $('#social-nav-button').html('Open');
        });
    }
);
    });
})(jQuery);

他们自己工作得很好。第一个按钮#mobile-nav-button 每次都能完美运行。第二个按钮#social-nav-button 也可以正常工作。但是,如果选择了#mobile-nav-button,则#social-nav-button 将停止工作(这不适用于相反的情况,因为#mobule-nav-button 将始终有效,即使#social-nav -按钮已被选中)。

我看过很多关于堆栈溢出的类似帖子(虽然不是同一个问题),但遗憾的是,我的 JS/jQuery 知识还远远不够好,无法对我的问题进行任何修复。

任何帮助将不胜感激!

【问题讨论】:

  • 那是因为您使用的方法已被弃用和删除,目前 toggle() 仅隐藏和显示元素。
  • 我使用的是旧版本的 jQuery (1.8)。我知道该方法从 1.9 开始已被删除,但我看到它的所有其他移动导航解决方案都让我头晕目眩,这是一个奇迹。

标签: javascript jquery button mobile


【解决方案1】:

您可以查看我的示例:Toggle Buttons show and hide

HTML:

<button type="button" id="mobile_bt">Mobile</button>
<button type="button" id="social_bt">Social</button>

使用 jQuery 的 JS:

$('#mobile_bt').click(function() {
    var help= $(this);
  $('#social_bt').toggle('slow', function() {
    help.hide();
    $(this).show();
  });
});

$('#social_bt').click(function() {
    var help= $(this);
  $('#mobile_bt').toggle('slow', function() {
    help.hide();
    $(this).show();
  });
});

【讨论】:

  • 可悲的是似乎对我不起作用。使我的一个按钮完全消失(因此我无法再次单击它以使其恢复原始状态)。这是我正在使用的原始代码,@ 987654322@ - 但是有两个按钮(一个在左边,如它所演示的那样),右边会有另一个做同样的事情,但如果这有意义的话,方向相反?
猜你喜欢
  • 2021-04-20
  • 1970-01-01
  • 2012-09-12
  • 2014-12-12
  • 2020-08-24
  • 2019-05-03
  • 2013-03-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多