【问题标题】:jQuery toggle function fails after a whilejQuery切换功能在一段时间后失败
【发布时间】:2015-07-18 11:20:16
【问题描述】:

我编写了一个 jQuery 切换函数,如下所示:

// category filter
$.fn.clickToggle = function(a, b) {
  var ab = [b, a];
  function cb(){ ab[this._tog^=1].call(this); }
  return this.on('click', cb);
};

$.fn.swapClass = function(oldClass, newClass) {
  return $(this).removeClass(oldClass)
                .addClass(newClass)
                .fadeIn('fast');
};

$('.category-filter').clickToggle(function() {
  // clear old
  $('.category-filter').each(function() {
    $(this).fadeTo('fast', 1.0);
    $(this).swapClass('label-success', 'label-default');
  })

  // get new
  var category = $(this).attr('data-category');
  $(this).swapClass('label-default', 'label-success');

  // apply new
  $('.category-filter').each(function() {
    if (!($(this).attr('data-category').indexOf(category) >= 0)) {
      $(this).fadeTo('fast', 0.5);
    }
  })
  $('.article_container').each(function() {
    if ($(this).attr('data-categories').indexOf(category) >= 0) {
      $(this).fadeIn('fast');
    } else {
      $(this).fadeOut('fast');
    }
  })
}, function() {
  $(this).swapClass('label-success', 'label-default');
  $('.article_container').each(function() { $(this).fadeIn('fast') })
  $('.category-filter').each(function() { $(this).fadeTo('fast', 1.0) })
});

它可以正常工作 - 请参阅 JSFiddle here - 但在单击一个链接然后“跳过”以单击另一个链接后,切换似乎会中断。

我的功能有什么问题?如何更改它以切换用户是再次单击同一链接还是下一次单击另一个链接?

【问题讨论】:

    标签: javascript jquery


    【解决方案1】:

    这是因为即使您在单击一个元素时更改了其他元素的样式,但您并没有更改._tog 的值。

    您正试图以两种不同的方式维护状态。一个通过 ._tog,另一个通过 label-success/label-default 类。我建议完全不要使用._tog 并依赖这些类。事实上,我会大大简化你的整个 javascript。

    更新对不起,这只是一个快速的答案,我必须运行,但你去吧: https://jsfiddle.net/uh123qwx/3/

    $('.category-filter').click(function(){
        $('.category-filter').not(this).removeClass('active');
        $(this).toggleClass('active');
    });
    .category-filter { background-color: #777; transition: all .5s ease-in-out; }
    .category-filter.active { color: green; background-color: #5cb85c; }
    
    .category-filter.active[data-category='custom article'] ~ div > .article_container:not([data-categories*='custom article']),
    .category-filter.active[data-category='retirement'] ~ div > .article_container:not([data-categories*='retirement']),
    .category-filter.active[data-category='property'] ~ div > .article_container:not([data-categories*='property'])
    {
        display:none;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <span class="label category-filter" data-category="custom article">custom article</span>
    <span class="label category-filter" data-category="retirement">retirement</span>
    <span class="label category-filter" data-category="property">property</span>
    
    <div>
      <div class="article_container" data-categories="custom article">
        <p>Custom</p>
      </div>
      <div class="article_container" data-categories="custom article">
        <p>Another custom</p>
      </div>
      <div class="article_container" data-categories="retirement">
        <p>Retirement</p>
      </div>
      <div class="article_container" data-categories="property">
        <p>Property</p>
      </div>
      <div class="article_container" data-categories="property retirement">
        <p>Property and retirement</p>
      </div>
    </div>

    【讨论】:

    • 感谢您的建议。知道如何实现吗?
    • 更新了一个简单的例子
    • 那是说真的太棒了。非常感谢!
    • 您的解决方案意味着我必须将这些类别硬编码到不理想的 CSS 中。
    【解决方案2】:

    您没有正确创建插件,并且可以让它们为您完成更多工作。

    First it's important to realize that when the selector matches more than one element...this in plugin includes the whole collection.

    因此,插件通常包含this.each() 部分以隔离实例。通常它看起来像return this.each(){ //instance code });

    不是在插件中迭代通常会完成的集合,而是在其他代码中对相同的元素集合进行大量迭代,而实际上不需要在那里

    【讨论】:

    • 我不确定我是否理解您的意思。我应该如何“正确”创建我的插件?您是指顶部的功能吗?
    • no ... 更多关于识别each 部分的信息。大多数繁重的工作和循环应该在插件中而不是在独立代码中......更不用说this.something() 会影响整个集合
    猜你喜欢
    • 2022-08-23
    • 2018-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多