繁体   English   中英

在遍历列表项时添加/删除类

[英]Add/remove class while traversing through list-items

我不太明白为什么这行不通。 我试图做到这一点, .on()单击将从列表项中.active-title class(). .active-title 然后将addClass(). .active-titlenext()列表项。

$(".next").on({
    click: function(){
        $('.active-title').removeClass(function(){
          $(this).next().addClass('active-title');
    }
  });
});

演示:

链接到JSFiddles演示


JSFIDDLES中的最终解决方案演示

您的代码中有一些错误

      }
   });
});

应该是

      }); <--- closing of removeClass
   } <-- closing for click function
});

尝试这个

$(".next").on({
    click: function () {
       // Find the actuve element
        var $active = $('.active-title', 'ul');
       // If the next element exists the get the element
       // otherwise get the first li from the ul
        var $nextElem = $active.next('li').length ? $active.next('li') 
                                                  : $('ul').find('li:first');
        // Add and remove class
        $active.removeClass('active-title');
        $nextElem.addClass('active-title');

    }
});

检查小提琴

暂无
暂无

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

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