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

Javascript 在遍历列表项时添加/删除类,javascript,jquery,onclick,traversal,Javascript,Jquery,Onclick,Traversal,我不太明白这为什么不起作用。我正试图使其成为这样,从列表项中单击将。活动标题;然后将.active title添加到列表项 $(".next").on({ click: function(){ $('.active-title').removeClass(function(){ $(this).next().addClass('active-title'); } }); }); 演示: 您的代码中有一些错误 } })

我不太明白这为什么不起作用。我正试图使其成为这样,从列表项中单击将
。活动标题
;然后将
.active title
添加到列表项

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



您的代码中有一些错误

      }
   });
});
应该是

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

单击JSFIDLE中的“JSHint”按钮。
$(".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');

    }
});